0
I need to create a kind of pause and resume, can be with Javascript (without jQuery).
I have a slide photos, each photo appears every 3 seconds with setTimeout()
and in this way (as they see the functions are chained to not happen delays):
setTimeout(function() {
//mostrar uma imagem
setTimeout(function() {
//mostra outra
setTimeout(function() {
//assim sucessivamente...
}, 3000);
}, 3000);
}, 0);
So I searched some help forums and found only one function that stops the script, but it wasn’t helpful because I need something to pause the setTimeout
and soon after I can give a resume again, I even tried a function Timer()
with object orientation, however it only works once it is not possible to chain the functions as I did with setTimeout()
.
See the tests I did and some functions I tried to use one more only worked once to pause the Timer()
, note that I added a onload to load some alerts if I use the Timer to pause they only pause one setTimeout, not pause all, and I want to pause all of them at once and when press on return it returns from where it stopped in setTimeout()
;
<!DOCTYPE html>
<html>
<head>
<title>Funções de Tempo</title>
<script type="text/javascript">
function alertas() {
// var timer = new Timer(function() {
// alert('Tempo Final');
// }, 10000);
setTimeout(function() {
alert('1 vez');
setTimeout(function() {
alert('2 vez');
setTimeout(function() {
alert('3 vez');
}, 3000);
}, 3000);
}, 3000);
}
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= Date.now() - start;
};
this.resume = function() {
start = Date.now();
window.clearTimeout(timerId);
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}
// var timer = new Timer(function() {
// alert("Done!");
// }, 1000);
var timer = new Timer(function() {
alert('ok');
}, 3000);
// timer.pause();
// Do some stuff...
// timer.resume();
function pausar() {
alert('pausado!');
timer.pause();
}
function continuar() {
alert('Retornando');
timer.resume();
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
</script>
</head>
<body onload="alertas()">
<button onclick="pausar()">Pausa</button>
<button onclick="continuar()">Retornar</button>
<!-- <button onclick="iniciar()">Iniciar</button> -->
</body>
</html>
can’t take a "break" in the
setTimeout
, What you can do is cancel with theclearTimeout
and control your break in a loop or event, and when the break is over, start again thesetTimeout
. Detail: to do this, associate thesetTimeout
to a variable– Ricardo Pontual
Thanks Ricardo, I’ll try to do this!
– Mateus R.
I did not quite understand the summary part, but in that case it would not be better to use setInterval() instead of setTimeout()?
– LeAndrade
I don’t know exactly how the setInterval() works, I almost thought that its function would be almost the same as the setTimeout
– Mateus R.
I can give a search on it, but what I really need is a button in the html that pause and the summary in setTimeout, the same as if it was a video you pause and play again to continue
– Mateus R.
It is not "abstract", it is "resumed".... the word "abstract" means "synthesis", something shorter, summarized. The English term "resume" does not mean "abstract". It’s a false cognate.
– Sam
Would that be https://jsfiddle.net/z7dsk32a/ ? - taking advantage,
setTimeout
runs something only once, whilesetInterval
runs indefinitely, or until interrupted by aclearInterval
. Finally, read: https://answall.com/q/77764/112052– hkotsubo