I'm looking for execute a function on nodejs with express for an exact time, and if the time have pass but the function don't end, the function stop.
I tried with setTimeOut. But it just works for execute a function after "x" time. I just need to execute the function for "x" time.
You could set a Boolean variable and change its value after "x" of time, while checking the same variable in your function.
var Continue = true; // Our Boolean variable.
var x = 5000; // Execute the code for amount of milliseconds.
function canContinue(){
return Continue;
}
var ourInterval = setInterval( function () {
if(canContinue())
console.log("Continue the code for one more second.")
else
clearInterval( ourInterval )
}, 1000 );
setTimeout( function () {
Continue = false;
}, x);
Hope that helps.