Reputation: 426
I want to disable function from running and enable it after certain timeout.
For example, I want to stop the first script from looping after a while and then start it again.
JS
// Script that loops
function messages() {
setInterval(function() {
setTimeout(function() {
alert("Hello!");
}, 2000);
setTimeout(function() {
alert("I said hello!");
}, 10000);
}, 20000); // It's looping every 20 sec
}
// Script with timeout to stop the loop
setTimeout(function() {
messages.stop();
}, 60000); // Stop it after 60 sec
setTimeout(function() {
messages.start();
}, 120000); // Start again it after 120 sec
Can this be done?
Upvotes: 0
Views: 94
Reputation: 75
I hope it will help you.
function messages() {
var main = setInterval(function() {
setTimeout(function() {
alert("Hello!");
}, 2000);
setTimeout(function() {
alert("I said hello!");
}, 10000);
}, 20000); // It's looping every 20 sec
setTimeout(function() {
clearInterval(main);
}, 60000); // stop again it after 60 sec
setTimeout(function() {
messages();
}, 120000); // start again it after 120 sec
}
messages();
UPDATED
Upvotes: 1
Reputation: 518
If it's inside a loop can't you:
// the start method turns a variable to true for example in the inner function scope
if (start) {
setTimeout((data) => data, 25000);
}
Would this work? Hope it helps.
Upvotes: 1