www.webdeveloper.com
Results 1 to 4 of 4

Thread: Delay between while loop iterations

  1. #1
    Join Date
    Jan 2012
    Posts
    6

    Delay between while loop iterations

    Hi all,

    I am trying to write a program to assist me in learning to play the piano. I have set up three buttons that will each call a function to randomly spit out chord names.
    The problem I am having is that I want a 3 second delay within the while loop between chord names. I have looked around on the net and can't seem to see how to do it. Here is the code for the first function:

    <html>
    <head>
    <script type="text/javascript">
    MAJORS = new Array('A','B','C','D','E','F','G');
    MINORS = new Array('Am','Bm','Cm','Dm','Em','Fm','Gm');
    MIXED = new Array('A','B','C','D','E','F','G','Am','Bm','Cm','Dm','Em','Fm','Gm');
    var iterations = 20;
    var timeDelay = 3000;


    function randomiseMajors(){
    var i =0;
    var rand;
    while(i<iterations){
    rand = parseInt(Math.floor(Math.random()*6));
    //alert(rand);
    document.writeln(MAJORS[rand]);
    i++;
    setTimeout(delay(),timeDelay);
    }
    }

    function delay(){
    }


    </script>
    </head>
    <body>
    <form>
    <input type="button" value="Majors" onclick="randomiseMajors()" />
    <p><input type="button" value="Minors" onclick="randomiseMinors()" /></p>
    <p><input type="button" value="Mixed" onclick="randomiseMixed()" /></p>
    </form>
    </body>
    </html>

    Also, is it easy to display the chord names one at a time? ie when a new chord is displayed the previous one disappears. (This question is not so important I am more interested in the Timeout question).

    Thanks for your help!

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,290
    Try setTimeout(delay,timeDelay); without () and do not use document.write (or writeln) which is made for an open document and is not valid in a function called after the document is close. Use for example a div with a innerHTML

    Code:
    // replace this
    document.writeln(MAJORS[rand]);
    // by 
    document getElementById('rsp').innerHTML+=MAJORS[rand];
    
    
    // with a
    <div id="rsp"></div>

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Something similar to what you want to do?

    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    
    			var
    				MAJORS = ['A','B','C','D','E','F','G'],
    				MINORS = ['Am','Bm','Cm','Dm','Em','Fm','Gm'],
    				MIXED  = ['A','B','C','D','E','F','G','Am','Bm','Cm','Dm','Em','Fm','Gm'],
    				delay  = 3000;
    
    			function randomiseKeys(keys)
    			{
    				var i = 0, e = document.getElementById('display');
    				function next()
    				{
    					var html = '', c;
    					for (c = 0; c < keys.length; c++) {html += '<span style="margin: 0 10px;' + (i === c ? 'text-decoration: underline;' : 'color: #888;') + '">' + keys[c] + '</span>';}
    					if (i < keys.length) {e.innerHTML = html + '<div style="font-size: 144px;">' + keys[i++] + '</div>' + html;} else
    					                     {e.innerHTML = ''; clearInterval(randomiseKeys.intID);}
    				}
    				keys.sort(function(){return Math.round(Math.random()) - 0.5;});
    				if (randomiseKeys.intID) {clearInterval(randomiseKeys.intID);}
    				randomiseKeys.intID = setInterval(next, delay);
    				next(); //SHOW FIRST CHORD IMMEDIATELY
    			}
    
    		</script>
    	</head>
    	<body>
    		<form>
    			<input type="button" value="Majors" onclick="randomiseKeys(MAJORS);" />
    			<input type="button" value="Minors" onclick="randomiseKeys(MINORS);" />
    			<input type="button" value="Mixed"  onclick="randomiseKeys(MIXED);"  />
    			<div id="display" style="text-align: center; margin-top: 50px;"></div>
    		</form>
    	</body>
    </html>
    Was fun
    Last edited by bionoid; 02-04-2012 at 10:24 AM.
    JavaScript: Learn | Validate | Compact | bionoid

  4. #4
    Join Date
    Jan 2012
    Posts
    6
    Thanks for that advice Julien, it displays better now and I can run it more than once. The time delay between iterations still doesn't work but that doesn't matter because I am going to dissect Bionoid's code and see how he did the time delay and try and replicate that.

    Thanks Bionoid, program works well. I am going to try and learn a few things from your code.

    Thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12982 seconds
  • Memory Usage 2,872KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates