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?

1
  • What about the fifth request and so on? Will the interval change? Commented Oct 8, 2011 at 9:39

1 Answer 1

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);
})();
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.