2016-12-07 70 views
1

我试图使代码的代码太多时间工作正常计算器,但是当我添加JavaScript代码,它有stopped.any方式 我有这样的代码:JavaScript的setTimeout的时间比预期EXCUTE

showCharacters("hey, how are you"); 
 

 
function showCharacters(text) { 
 
    var charactersArray = new String(text).split(''); 
 
    var i; 
 
    for (i = 0; i < charactersArray.length; i++) { 
 
    setCharacter(i, charactersArray); 
 
    } 
 
    deleteAll(i - 1); 
 
} 
 

 
function setCharacter(i, charactersArray) { 
 
    setTimeout(function() { 
 
    document.getElementById("characters").innerHTML = document.getElementById("characters").innerHTML + charactersArray[i]; 
 
    }, 1000 * i); 
 
} 
 

 
function deleteAll(i) { 
 
    setTimeout(function() { 
 
    var text = document.getElementById("characters").innerHTML; 
 
    var charactersArray = new String(text).split(''); 
 
    var charactersArrayLength = charactersArray.length - 1; 
 
    for (var j = charactersArrayLength; j >= 0; j--) { 
 
     charactersArray.splice(j, 1); 
 
     deleteCharacter(charactersArray.join(""), i + charactersArrayLength - j + 1); 
 
    } 
 
    }, (i + 1) * 1000); 
 
} 
 

 
function deleteCharacter(text, i) { 
 
    setTimeout(function() { 
 
    document.getElementById("characters").innerHTML = text; 
 
    }, 1000 * i); 
 
}
#divContainer { 
 
    display: flex; 
 
    align-items: center; 
 
    float: right; 
 
} 
 
#characters { 
 
    font-weight: 700; 
 
    color: rgb(0, 0, 0); 
 
    font-size: 40px; 
 
    padding-top: 2px; 
 
    white-space: nowrap; 
 
}
<div id="divContainer"> 
 
    <h1 id="characters"> 
 
    </h1> 
 
</div>

我做的代码写一个字符每秒,完整的文本后,会删除一个字符每秒,问题是,完成文字它将等待几秒钟,开始删除前,后人物。我没有那样做。

我跟着settimeout方法的时间值,一切都很好。

我希望它在完成文本后直接开始删除字符。

请任何帮助。

+0

一对夫妇一般的言论:请编辑代码片段时,使用[整洁]按钮,它使代码更易读。另外,我建议让变量名缩短一点,例如使用'i'或'j'作为索引是非常常见的,比现在所有的文本都要好。 –

+0

/*编辑后:* /谢谢,更好 –

+0

哦,谢谢你。你欢迎你 – phoenix

回答

0

我贴近你的代码,修复实际上是相当简单的,我下面THE FIX标记它。

// Replace spaces with non-breaking spaces so it does not stutter, and use global var 
 
var text = "hey, how are you".replace(/ /g, String.fromCharCode(160)); 
 
showCharacters(); 
 

 
function showCharacters() { 
 
    var chars = new String(text).split(''); 
 
    var i; 
 
    for (i = 0; i < chars.length; i++) { 
 
    setCharacter(i, chars); 
 
    } 
 
    deleteAll(i - 1); 
 
} 
 

 
function setCharacter(i, chars) { 
 
    setTimeout(function() { 
 
    document.getElementById("characters").innerHTML = document.getElementById("characters").innerHTML + chars[i]; 
 
    }, 200 * i); 
 
} 
 

 
function deleteAll(i) { 
 
    setTimeout(function() { 
 
    var chars = new String(text).split(''); 
 
    var len = chars.length - 1; 
 
    for (var j = len; j >= 0; j--) { 
 
     chars.splice(j, 1); 
 
     deleteCharacter(chars.join(""), i - j + 1); // THE FIX: don't add length here 
 
    } 
 
    }, (i + 1) * 200); 
 
} 
 

 
function deleteCharacter(text, i) { 
 
    setTimeout(function() { 
 
    document.getElementById("characters").innerHTML = text; 
 
    }, 200 * i); 
 
}
#divContainer { 
 
    display: flex; 
 
    align-items: center; 
 
    float: right; 
 
} 
 
#characters { 
 
    font-weight: 700; 
 
    color: rgb(0, 0, 0); 
 
    font-size: 40px; 
 
    padding-top: 2px; 
 
    white-space: nowrap; 
 
}
<div id="divContainer"> 
 
    <h1 id="characters"> 
 
    </h1> 
 
</div>

+0

替换方法中的参数意味着什么? – phoenix

+0

第一部分'//g'是一个非常简单的正则表达式,它匹配空格,并且它将全局匹配(因此'g')来实现全局替换。第二部分是我们刚刚找到的空格将被替换的内容,它是一个库调用来获得一个ASCII值为160的字符。在HTML中,字符与使用' '相同,但只有一个字符,这是我们在这里需要的。 –

+0

是的,你是对的,非常感谢。但为什么这就是这个问题(时间问题)的原因。?再次感谢 – phoenix

1

不要排队应用程序连续超时应用程序做他们一个接一个。

下面是你应该怎么做。我加快了一点,但事实并非如此。

var text = "Add a new timeout as needed." 
 
var div = document.createElement("div"); 
 
document.body.appendChild(div); 
 
var textPos = 0; 
 
function textAdd(){ 
 
    div.textContent += text[textPos++]; 
 
    if(textPos >= text.length){ 
 
     setTimeout(clearText,1000); 
 
    }else{ 
 
     setTimeout(textAdd,200); 
 
    } 
 
} 
 
function clearText(){ 
 
    textPos --; 
 
    div.textContent = text.substr(0,textPos); 
 
    if(textPos === 0){ 
 
     setTimeout(textAdd,1000); 
 
    }else{ 
 
     setTimeout(clearText,200); 
 
    } 
 
} 
 
textAdd();

+0

我在问原因,而不是一个好的代码。 但你的代码是惊人的,并且非常感谢。 – phoenix

+0

@phoenix我看着你的代码,不能在没有运行它的情况下检查错误的超时错误。您可以使用控制台显示信息。如果在超时之前添加'console.log(“timeout Func name”+((i + 1)* 1000));'然后用F12打开控制台,您将看到时间列表。这将帮助您跟踪问题。 – Blindman67

+0

我做到了,一切都很好,我在帖子中说过。 在完成文本和开始删除字符 – phoenix