1

I am using polling with Javascript to get some information with ajax but in exact the same time intervals.

intervalRequestId = setInterval('loadInfo()', 2500);

Because I know now, that the information is coming in different time intervals, I want to make the polling in different time intervals. For example:

First request: after 2 seconds - > very importand that is after 2 seconds! Second request: after 7 seconds Third request after 15 seconds Fourth request after 25 seconds

Is there good possibility?

user229044
  • 232,980
  • 40
  • 330
  • 338
Mutatos
  • 1,675
  • 4
  • 25
  • 55

1 Answers1

1
(function(){
var iteration = 0, // keep track of your iterations
    delays = [7000, 15000, 25000], // delays between iterations
    callback; // wrapper for your loadinfo()

callback = function(){
    // run your function
    loadinfo();
    // determine next delay, if not set, use the last of the sequence
    var delay = delays[iteration] || delays[delays.length - 1];
    // next run, next iteration
    iteration++;
    // register callback to run after determined delay
    setTimeout(callback, delay);
};

// run first execution after 2 seconds
setTimeout(callback, 2000);
})();
rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Cool stuff, thanks! I have made it in the other way. I am counting the iterations as well and only if some iteration is acceptable, only then, the ajax call is executed. I will post my answer as well. – Mutatos Oct 08 '11 at 16:34