Polling with Javascript, but in different time intervals
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 i开发者_高级运维mportand that is after 2 seconds! Second request: after 7 seconds Third request after 15 seconds Fourth request after 25 seconds
Is there good possibility?
(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);
})();
精彩评论