2016-12-07 42 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

相關問題