Javascript: Pause Settimeout();

javascript: pause setTimeout();

You could wrap window.setTimeout like this, which I think is similar to what you were suggesting in the question:

var Timer = function(callback, delay) {
var timerId, start, remaining = delay;

this.pause = function() {
window.clearTimeout(timerId);
timerId = null;
remaining -= Date.now() - start;
};

this.resume = function() {
if (timerId) {
return;
}

start = Date.now();
timerId = window.setTimeout(callback, remaining);
};

this.resume();
};

var timer = new Timer(function() {
alert("Done!");
}, 1000);

timer.pause();
// Do some stuff...
timer.resume();

How to pause setTimeout and then continue it?

In this case, you may try using setInterval instead of setTimeout.

About setInterval:

https://www.w3schools.com/jsref/met_win_setinterval.asp

How do I pause and resume multiple setTimeout in JavaScript?

You need to declare and set remaining and start separately.

var start_yT,start_gT, remaining_yT, remaining_gT;


function pause() {
window.clearTimeout(yT);
window.clearTimeout(gT);
remaining_yT -= Date.now() - start_yT;
remaining_gT -= Date.now() - start_gT;
};

function resume() {
start = Date.now();
window.clearTimeout(yT);
window.clearTimeout(gT);
yT = window.setTimeout(function(){
all();
loop();
}, remaining_yT);
gT = window.setTimeout(function(){
all();
loop();
}, remaining_gT);

};

pause();
resume();

And if you need to do with more advance version of pause/resume, this is the
link . This create each timer separately runtime as we did manually.

How to pause a setTimeout function

var Timer = (function () {    var isExeced = [1,1,1];    var timeout1, timeout2, timeout3;    var startTime, costTime = 0;    var finished = false;    var status = 0;
function reset () { costTime = 0; status = 0; finished = false; isExeced = [1,1,1]; }
function desertAK () { document.getElementById("leser").innerHTML = '<iframe src="https://opskins.com/?loc=shop_search&sort=lh&app=433850_1&search_item=%22Desert%20Warfare%20AK-47%22"></iframe>'; }
function fourAlarmShotgun () { document.getElementById("leser2").innerHTML = '<iframe src="https://opskins.com/?loc=shop_search&sort=lh&app=433850_1&search_item=%22Four%20Alarm%2012GA%20Pump%20Shotgun%22"></iframe>'; }
function frostbiteAR () { document.getElementById("leser").innerHTML = '<iframe src="https://opskins.com/?loc=shop_search&sort=lh&app=433850_1&search_item=%22Frostbite%20AR-15%22"></iframe>'; }
function timedText () { if (finished) { return resume(); }
if (status === 1) { return; } else { status = 1; }
startTime = +new Date(); if (isExeced[0]) { timeout1 = setTimeout(function () { isExeced[0] = 0; desertAK(); }, 1000 - costTime); }
if (isExeced[1]) { timeout2 = setTimeout(function () { isExeced[1] = 0; fourAlarmShotgun(); }, 5000 - costTime); }
if (isExeced[2]) { timeout3 = setTimeout(function () { isExeced[2] = 0; finished = true; status = 0; frostbiteAR(); }, 9000 - costTime); } }
function stopTimeout () { costTime += +new Date() - startTime; status = 0;
clearTimeout(timeout1); clearTimeout(timeout2); clearTimeout(timeout3); }
function resume () { stopTimeout(); reset(); timedText(); }
return { start: timedText, stop: stopTimeout, resume: resume }})();

How to resume a set timeout in JavaScript?

Kiran linked another question which is probably a good solution for you.

As mentioned, there's no built-in pauseTimeout function.

However, a simpler way would be to have an if statement within your setTimeout. Something like:

let timeoutPaused = false;

setTimeout(function(){
if (!timeoutPaused) {
doSomething();
}
}, 1000)

function toggleTimeout() {
timeoutPaused = !timeoutPaused;
}

Then you can just call toggleTimeout when you want to turn it off or on. Plus you don't even need that as a separate function really, I just put it like so for educational purposes.

How to pause a setTimeout call?

Try this.

var myTimeOut;
$(someElement).mouseout( function () {
myTimeOut = setTimeout("mytimeoutfunction()", 5000)
});

$(someElement).mouseover( function () {
clearTimeout(myTimeOut);
});

How to pause running SetTimeOut Function?

You can keep track of the latest timeout using a variable, and then cancel the timeout using clearTimeout:

var timeout;

document.getElementById("a").addEventListener("mouseover", stop);
if (document.readyState === "complete") {
auto()
}

var d = document.getElementById("a");

function stop() {
d.style.backgroundColor = "brown";
clearTimeout(timeout);
}

function auto() {
timeout = setTimeout(function() {
d.style.backgroundColor = "blue";

if (d.style.backgroundColor === 'blue') {
timeout = setTimeout(function() {
d.style.backgroundColor = "yellow";

if (d.style.backgroundColor === 'yellow') {
timeout = setTimeout(function() {
d.style.backgroundColor = "red";

if (d.style.backgroundColor === 'red') {
return auto();
}

}, 1000)
}
}, 1000)
}
}, 1000)

};


Related Topics



Leave a reply



Submit