SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with setTimeout and arrays

    Basically on the load of a page I am making an ajax call that returns a string with integers seperated by commas. I have verified that the returns from the ajax call are working correctly, but then I am trying to take that string, break it into an array, and use the values in that array to use setTimeout and call the function at those intervals.

    Here's my code:

    Code:
    //Create our array outside of a function so it's available
    var wait_time = new Array();
    
    //function that handles the ajax return
    function process_wait_time_return(str)
    {
        wait_time = str.responseText.split(',');
        update_current_show(0, wait_time);
    }
    
    //Function that changes the div html with new information
    function update_current_show(array_index, wait_time)
    {
        $("currently_playing").innerHTML = array_index;
        var next_index = array_index + 1;
        setTimeout('update_current_show(next_index, wait_time)', 3);
    }
    This is on a test page, so for testing purposes I'm just updating the div with the current array index to verify it's cycling through the array, which it's not.

  2. #2
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your setTimeout function will have a problem with the next_index variable because it is not global. Remember, the first parameter to setTimeout is a string. Replace the 'next_index' with the content of the variable or make the variable global and you'll get better results.

    mikem

  3. #3
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So even though I pass it the variable 'next_index' through the function call it can't read it because it's initialized inside the function?

    I don't quite understand how I should fix this, do I initialize the variable outside of a function like I do with the variable 'wait_time'?

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jhorra View Post
    So even though I pass it the variable 'next_index' through the function call it can't read it because it's initialized inside the function?
    Almost. The problem with your original code was that you *didn't* pass in the variable, instead, you passed in a string which happened to have the name of the variable in it.

    Quote Originally Posted by Jhorra View Post
    I don't quite understand how I should fix this, do I initialize the variable outside of a function like I do with the variable 'wait_time'?
    The best solution is to pass a function to setTimeout instead of a string, then you can pass in whichever variables you like.

    Douglas
    Hello World

  5. #5
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX View Post
    Almost. The problem with your original code was that you *didn't* pass in the variable, instead, you passed in a string which happened to have the name of the variable in it.
    Douglas
    Doesn't this: wait_time = str.responseText.split(',');
    Create an array from the string it gets initially?

    Also, in your function, do I need to replace
    //Local variables with something?

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jhorra View Post
    Doesn't this: wait_time = str.responseText.split(',');
    Create an array from the string it gets initially?
    Yes, but the problem is this line:

    setTimeout('update_current_show(next_index, wait_time)', 3);
    Because

    Code:
    'update_current_show(next_index, wait_time)'
    is a string.
    Hello World

  7. #7
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As mikem says, your setTimeout is wrong. Here is an "expanded" version of your code, this is closer to what the browser sees you doing:

    Code:
    //Function that changes the div html with new information
    function update_current_show(array_index, wait_time)
    {
        $("currently_playing").innerHTML = array_index;
        var next_index = array_index + 1;
        setTimeout(doeval, 3);
    }
    function doeval() {
        eval('update_current_show(next_index, wait_time)';
    }
    Or

    Code:
    //Function that changes the div html with new information
    function update_current_show(array_index, wait_time)
    {
        $("currently_playing").innerHTML = array_index;
        var next_index = array_index + 1;
        setTimeout(doeval, 3);
    }
    function doeval() {
       // looking for global variables 
       update_current_show(next_index, wait_time);
    }
    A better way to write it would be to use a function instead of a string to capture the variables:

    Code:
    //Function that changes the div html with new information
    function update_current_show(array_index, wait_time)
    {
        $("currently_playing").innerHTML = array_index;
        var next_index = array_index + 1;
        setTimeout(function() {
            // local variables
            update_current_show(next_index, wait_time);
        }, 3);
    }
    hth,
    Douglas
    Hello World

  8. #8
    SitePoint Evangelist Jhorra's Avatar
    Join Date
    Sep 2004
    Location
    Phoenix, Az
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Update, passing it a function worked. I don't completely understand what the problem was, but thank you everyone for your suggestions and help


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
  •