我正在製作Nest的Smoke and Carbon Monoxide Alarm Nest Protect的模擬器。但是當我按下按鈕(點擊)內環不會按照它應該變成藍色。它說話,就像我使用ResponsiveVoice一樣,但它不會亮起來!這是我的(未完成的)代碼。Javascript img change聲明過早過早
<script src="http://code.responsivevoice.org/responsivevoice.js"></script>
<script>
function delay(millis) {
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}
function press() {
document.getElementById("unit").src = "assets/img/blue.jpg";
delay(500);
responsiveVoice.speak("Ready. In the Living Room. Press to test.");
delay(500);
document.getElementById("unit").src = "assets/img/idle.jpg";
}
</script>
因爲JavaScript運行在單個線程上,並且如果JavaScript執行停留在while循環中,您應該真的考慮使用'setTimeout'來代替硬延遲,頁面將凍結。 – yvesmancera