3

My setTimeout function does not want to work. I am not getting any of the alerts, so I'm assuming I did something incredibly stupid.

var timeout = 1000;
for(k=0;k<pages.length;k++)
{
    randomnumber=Math.floor(Math.random()*pages[k].length);
    setTimeout(function() {
        $.ajax({
            type: 'GET',
            url: pages[k][randomnumber],
            success: function(data) {
                alert(data);
                if(data.indexOf('VIDEO_LENGTH') > 0)
                {
                    timeouttext = data.substr(data.indexOf('VIDEO_LENGTH')+12);
                    timeouttext = timeouttext.substr(timeouttext.indexOf('.'));
                    timeout = parseInt(timeouttext);
                    alert(timeout);
                }
                else
                    timeout = 1000;
                $('#loader').hide('fast','fade');
                $('#information').html(data);
                $('#information').show('fast','fade');
            }
        })
    },timeout);
    alert("PAGE " + k + " RandomNumber " + randomnumber + " : " + pages[k][randomnumber]);
    if(k==3) {
        k = 0;
    }
}

Can anyone assist me.

Bird87 ZA
  • 2,313
  • 8
  • 36
  • 69
  • When you post questions, and you reference things like "data." Its always useful to post what data looks like... – Nix Mar 19 '12 at 12:28
  • data is a simple text/html file that get's loaded with the ajax request. – Bird87 ZA Mar 19 '12 at 12:45

4 Answers4

2

EDIT: ok follow error:

the var k when you call the page url is not as expected. So try:

{
    randomnumber=Math.floor(Math.random()*pages[k].length);
    var selectedIndex = k; // store current k
    setTimeout(function() {

    $.ajax({
        type: 'GET',
        url: pages[selectedIndex][randomnumber], // use stored k
 ...
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
1

When you debug your code using FireBug or CHrome's developer tools do you see your Ajax requests?

It looks to me like your never getting to the "success" event for each request. Since you have no "error" event in your Ajax request you'd never see any feedback.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
1

Your setTimeout is correct. To further check for errors:

  1. check in console whether your request is being sent.

  2. add error: function(){} or complete:function(){} block which logs the status of the request, to see, if your server-side breaks your code.

edit: (i suggest you don't use the shorthand if, it's very error-prone.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • I added an error block after LastCoder's answer. I get no response from that. AFAIK, the request is being sent (according to console) – Bird87 ZA Mar 19 '12 at 13:14
  • correct your if-condition missing `{` (check if the console promts any errors), and add an `console.log()` according to [ajax-Docs](http://api.jquery.com/jQuery.ajax/) in the complete- or error-block to see, which status is returned. – Christoph Mar 19 '12 at 13:18
0

Try this (Updated) fiddle here.

var funfacts= ['URL1','URL2','URL3'];
var multimedia= ['URL4','URL5'];
var employees = ['URL6','URL7','URL8','URL9','URL10'];
var information = ['URL11','URL12','URL13'];

var pages = [funfacts, multimedia, employees, information];
var timeout=1000;
var t=0;
for(i=0;i<pages.length;i++)
{
    (function(value){
        for(j=0;j<pages[value].length;j++)
        {
            t++;
            (function(iv, ij){
                var tmo = timeout*t;
                setTimeout(function(){
                    callAjax(iv, ij);
                }, tmo);

            })(value, j); 
        }

    })(i);             
}

function callAjax(i, j)
{
    $.ajax({
        type: 'GET',
        url: pages[i][j],
        success: function(data) {    
            if(data.indexOf('VIDEO_LENGTH') > 0) // indexOf should be checked if(var.indexOf('sometext')!=-1)
            {
                // your code
            }
            else
            {
                // your code
            }
        }
    });
}

​If everything else is fine then it should work.

you can also read this and this on SO.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thanks for the answer. Yours is the one that works best so far. But I still have an issue where there is no pause between the ajax calls. My console shows this: `GET http://localhost/info_screens/content/funfacts/0.html GET http://localhost/info_screens/content/multimedia/0.html GET http://localhost/info_screens/content/employees/0.html GET http://localhost/info_screens/content/information/0.html` BUT there is no pause between any of those. – Bird87 ZA Mar 19 '12 at 14:51
  • Just a comment on the `if(data.indexOf...` note: I know for a fact that `'VIDEO_LENGTH'` will never be on position 0. Although I know this, it's still not good coding practice, so I changed it. Thanks for pointing it out. – Bird87 ZA Mar 19 '12 at 14:55
  • Oops I've made a mistake, the "k" should be "i", url: pages[k][randomnumber] should be url: pages[i][randomnumber] – The Alpha Mar 19 '12 at 15:52
  • I picked up the last error. :) Async false does nothing. Still loads all the pages simultaneously and displays the last one... – Bird87 ZA Mar 19 '12 at 15:55
  • Another thing I noted, it doesn't reset k to 0 either. – Bird87 ZA Mar 19 '12 at 15:56
  • try this, at last of settimrout },timeout*i)})(i); – The Alpha Mar 19 '12 at 15:57
  • woooohoooo!! That did it. BUT, k is still not resetting to 0, so it's not fetching a new page from any of the pages arrays... Do you think I need to reset i to 0 too? – Bird87 ZA Mar 19 '12 at 16:00
  • Can you do this test, if(k==3) { k=0;console.log(k);} – The Alpha Mar 19 '12 at 16:03
  • ok, k return as 0 after the fourth iteration which is correct. From what I can tell in the code, i needs to be 0, but I'm sure that's going to cause the freezing issue again. – Bird87 ZA Mar 19 '12 at 16:06
  • hmmm... so now I'm back to square one... How do I restart the for-loop? – Bird87 ZA Mar 19 '12 at 16:09
  • Basically, each `pages` array contains an array of URLs. These URLs are devided into 4 categories. The number of URLs are unknown. So, I grab a random URL from a category, and display that URL on screen. When the timeout event fires, it fetches the next category's random URL. I don't know if I'm clear enough... – Bird87 ZA Mar 19 '12 at 16:12
  • Can you post how your pages array looks like ? – The Alpha Mar 19 '12 at 16:15
  • `Array ( funfacts => Array ('URL1','URL2','URL3') , multimedia => Array ('URL4','URL5') , employees => Array ('URL6','URL7','URL8','URL9','URL10') , information => Array ('URL11','URL12','URL13') )` - I tried to make it easy to understand. One thing to note is that it won't just have 4 or 5 URLs, it'll have a lot! – Bird87 ZA Mar 19 '12 at 16:19
  • This is not a javascript array ! – The Alpha Mar 19 '12 at 16:28
  • In javascript there is no associative array but array of objects. – The Alpha Mar 19 '12 at 16:30
  • I only sent that to make it easier to understand. funfacts, multimedia, employees and information are all arrays. And I use an array to put all those arrays in. So `pages` is defined as `var pages = new Array(funfacts, multimedia, employees, information);` and each of those arrays have string URLs in them. – Bird87 ZA Mar 19 '12 at 16:38
  • Can you try this http://jsfiddle.net/jG9Gn/4/ – The Alpha Mar 19 '12 at 18:03
  • It throws all 4 values at the same time. – Bird87 ZA Mar 20 '12 at 06:11