SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setTimeout Problem

    Hello. I wish to alert the names that resides in an array with 5 seconds intervals. But I cant retrieve names from array appropriately . How can I solve this?
    Thanks in advance.

    var names=["Keol","Jol","Roserin"];

    function director (index) {

    alert(names[index]);
    }

    for ( var i=0;i<names.length;i++) {
    var b=5000*(i+1);

    window.setTimeout("director(i)", b);

    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by owerio View Post
    Hello. I wish to alert the names that resides in an array with 5 seconds intervals. But I cant retrieve names from array appropriately . How can I solve this?
    Thanks in advance.
    Code:
    setTimeout("director("+i+")", b);
    Gratitude assumed in advance.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much @Logic Ali.

  4. #4
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello @Ali. Same problem but for greasemonkey. I cant pass i variable appropriately to anonymous function.

    var names=["Keol","Jol","Roserin"];
    for ( var i=0;i<names.length;i++) {
    var b=5000*(i+1)
    window.setTimeout(function() {alert(names[i]);}, b);
    }

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because i is changed on each iteration of the loop. Create a closure:
    Code:
    var names=["Keol","Jol","Roserin"];
    var createCallback = function(i) {
      return function() {alert(names[i]);}
    }
    for (var i=0;i<names.length;i++) {
      var b = 5000 * (i+1);
      window.setTimeout(createCallback(i), b);
    }

  6. #6
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much @kyberfabrikken. Solved my problem.

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only reason Logic's code may have failed is if director is not defined in the window scope,
    where the setTimeout is looking for it.

    Code:
    function setTimer(){
    	var names= ["Keol","Jol","Roserin"], i=0;
    	window.director= function (index) {
    		alert(names[index]);
    	}
    	while(names[i]) {
    		window.setTimeout("director("+i+")", 5000*(++i));
    	}
    }
    // test case:
    setTimer()
    Last edited by mrhoo; Oct 13, 2007 at 09:12. Reason: added test case

  8. #8
    SitePoint Member Mupo's Avatar
    Join Date
    Oct 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good experiance, mrhoo . Regards!


Bookmarks

Posting Permissions

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