在写这篇文章之前,我已经看到了下面提到的问题。
how-to-pause-a-settimeout-call
how-to-pause-a-settimeout-function
how-to-pause-a-function-in-javascript
delay-running-a-function-for-3-seconds
问题
下面是我想要在一段时间后暂停我的setTimeout函数的代码。那就再继续。
typingCallback(that) {
let total_length = that.typewriter_text.length;
let current_length = that.typewriter_display.length;
if (that.rewind === false)
{
if(total_length == current_length)
{
// Here I want to pause this function for 3 seconds
clearTimeout(3000);
that.rewind = true;
}
else
{
that.typewriter_display += that.typewriter_text[current_length];
console.log('Loop#1: ' + that.typewriter_display + " " + 'Length' + current_length);
}
}
else if (that.rewind === true)
{
if(current_length == 0)
{
that.rewind = false;
}
else
{
that.typewriter_display = that.typewriter_display.slice(0, -1);
console.log('Loop#2: ' + that.typewriter_display + " " + 'Length' + current_length);
}
}
setTimeout(that.typingCallback, 75, that);
}发布于 2017-07-18 07:54:49
基本上,一个简单的setTimeout应该这样做:
typingCallback(that) {
let total_length = that.typewriter_text.length;
let current_length = that.typewriter_display.length;
if (that.rewind === false)
{
if(total_length == current_length)
{
// Here I want to pause this function for 3 seconds
setTimeout(that.typingCallback,3000,that);//simply set the 3 second timeout
that.rewind = true;
return;//dont forget to stop the function somewhere here. If not were having two recursive timeout chains... :/
}
//call directly
setTimeout(that.typingCallback,75,that);// the regular timeout发布于 2017-07-18 07:54:50
试试这个..。
function live() {
if (dead) {
return;
}
// do something when alive
setTimeout(live,speed);
}
live();希望这对你有用..。:)
https://stackoverflow.com/questions/45160542
复制相似问题