I need to have a never ending loop in javascript. This will be almost like set interval. But for each loop the time delay need to be applied dynamically. The delay value will be changing on each loop. So is it possible to use while with some kind of sleep function? Or is it possible to change the set interval time for each iteration?
following code didn't give the delay. Always take the first value given to it.
<script>
var someTimeoutVar = 1000;
var inc = 1;
setInterval(function() {
inc++;
someTimeoutVar = inc * 1000;
console.log(someTimeoutVar);
}, someTimeoutVar)
</script>
I need to have a never ending loop in javascript. This will be almost like set interval. But for each loop the time delay need to be applied dynamically. The delay value will be changing on each loop. So is it possible to use while with some kind of sleep function? Or is it possible to change the set interval time for each iteration?
following code didn't give the delay. Always take the first value given to it.
<script>
var someTimeoutVar = 1000;
var inc = 1;
setInterval(function() {
inc++;
someTimeoutVar = inc * 1000;
console.log(someTimeoutVar);
}, someTimeoutVar)
</script>
Share
Improve this question
edited Dec 19, 2014 at 4:23
sugunan
asked Dec 19, 2014 at 4:10
sugunansugunan
4,4667 gold badges45 silver badges67 bronze badges
1
- Sure ... You can create a randomizer function and for every iteration of the loop call the function... What have you tried? – Edward J Beckett Commented Dec 19, 2014 at 4:14
3 Answers
Reset to default 8Use setTimeout instead and keep recursive call.
function someLoop() {
var nextExecutionTime = calc();
setTimeout(someLoop, nextExecutionTime);
}
You can use:
setInterval(expression, someTimeoutVar);
and then change your someTimeoutVar as you need to
function repeatedOperation() {
/* Do operations here. */
/* ... */
/* You can return something if desired to help
* the other function decide what the new timeout
* delay should be. By default, I have set the new
* timeout delay to be whatever is returned here
* plus a random fuzz time between 0-1 second. */
return 1; /* interpreted as milliseconds */
}
function updateTimeout(result) {
/* Compute new timeout value here based on the
* result returned by the operation.
* You can use whatever calculation here that you want.
* I don't know how you want to decide upon a new
* timeout value, so I am just giving a random
* example that calculates a new time out value
* based on the return value of the repeated operation
* plus an additional random number. */
return Math.round(result + 1000*Math.random());
/* This might have been NaN. We'll deal with that later. */
}
function proxyInterval(oper, delay) {
var timeout = delay(oper());
setTimeout(function() { proxyInterval(oper, delay); }, timeout-0 || 1000);
/* Note: timeout-0 || 1000 is just in case a non-numeric
* timeout was returned by the operation. We also
* could use a "try" statement, but that would make
* debugging a bad repeated operation more difficult. */
}
/* This is how to use the above code: */
proxyInterval(repeatedOperation, updateTimeout);