這是我第一次在實際應用中使用while循環,所以請原諒我的無知。setTimeout導致無限while循環
我正在創建一個網頁,演示隨着時間的推移使用燈泡的成本。
在此階段,我嘗試使用while循環更新並顯示用戶單擊燈開關後經過的小時數。 (1小時代表實時1秒)
當我在螢火蟲上設置斷點時,一切正常運行,直到我在while循環中找到setTimeout方法。在setTimeout方法中斷之後,我單擊繼續,它會立即再次在同一個地方中斷,而不會執行其他任何事情。
當我沒有設置斷點時,它凍結了firefox,我不得不停止腳本執行。
我重新檢查,以確保我正確使用setTimeout。現在我甚至不知道在哪裏檢查或搜索什麼,因爲我不明白髮生了什麼問題。即使只是我可能檢查或研究的暗示,也會非常有幫助。
我試圖儘可能詳細地評論代碼。如果需要,我會很樂意澄清一些事情。
我會強烈建議在的jsfiddle考慮看看:
但這裏是我的代碼:
我的JS
$(document).ready(function() {
//set image to default off position
$('#lightswitch').css("background-image", "url(http://www.austinlowery.com/graphics/offswitch.png)");
// setup the lifetime hours of the lightbulb for later use
var lifetimeHours = 0;
// setup function to update the calculated lifetime hours number on the webpage to
// be called later
function updateLifetimeHoursHtml (lifetimeHours) {
$('#lifetimeHours').html(lifetimeHours);
}
// set up function to to send to setTimeout
function updateNumbers() {
// increment lifetimeHours by one
lifetimeHours = lifetimeHours++;
// call function to update the webpage with the new number result
updateLifetimeHoursHtml(lifetimeHours);
}
// When the lightswitch on the webpage is clicked, the user should see the
// lifetime hours update every second until the user clicks the switch again
// which will then display the off graphic and pause the updating of the lifetime
// hours
$('#lightswitch').click(function(){
// if the lightswitch is off:
if ($('#lightswitch').attr('state') == 'off') {
// set switch to on
$('#lightswitch').attr('state', 'on');
// update graphic to reflect state change
$('#lightswitch').css("background-image", "url(http://austinlowery.com/graphics/onswitch.png)");
// start updating the lifetime hours number on the webpage
// while the #lightswitch div is in the on state:
while ($('#lightswitch').attr('state') == 'on'){
//call update numbers every second
setTimeout('updateNumbers()', 1000);
}
// the lightswich was not in the off state so it must be on
}else{
// change the state of the switch to off
$('#lightswitch').attr('state', 'off');
// update graphic to reflect state change
$('#lightswitch').css("background-image", "url(http://austinlowery.com/graphics/offswitch.png)");
};
});
});
我的HTML
<div id="container">
<div id="lightswitch" state="off"> </div>
<span>After </span><span id="lifetimehours">0</span><span> lifetime hours:</span>
<br><br>
<span><b>You have spent:</b></span>
<br><br>
<span id="dollaramoutelectricity"></span><span> on electricty</span>
<br>
<span id="mainttime"></span><span> on maintenace</span>
<br>
<span id="dollaramountbulbs"></span><span> on replacement bulbs</span>
<br><br>
<span><b>You have:</b></span>
<br><br>
<span>Produced </span><span id="amountgreenhousegasses"></span><span> of greenhouse gasses</span>
<br>
<span>Sent </span><span id="amounttrash"></span><span> of trash to the dump</span>
<br>
<span>Used </span><span id="amountelectricty"></span><span> of electricity</span>
</div>
'while(stuff){setTimeout(...)}' - 您通過這種方式設置無限超時 - 嘗試使用if。 – moonwave99
有點偏離主題,但沒有必要這麼多評論。任何閱讀'$('#lightswitch')。attr('state','on');'的人都會知道你將開關設置爲'on',而沒有解釋它的註釋。他們只是混淆了代碼。 – peacemaker
對'setTimeout()'的調用將不起作用 - 您只需傳遞函數的名稱,而不是該字符串。 (反正不會真的有效,但這是問題的一部分。) – Pointy