2011-12-29 99 views
0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript"> 
<!-- 

function init() 
{ 
    timeDisplay = document.createTextNode (""); 
    document.getElementById("clock").appendChild (timeDisplay); 
} 

function updateClock() 
{ 
    var currentTime = new Date(); 

    var currentHours = currentTime.getHours(); 
    var currentMinutes = currentTime.getMinutes(); 
    var currentSeconds = currentTime.getSeconds(); 

    // Adds zeros if required 
    currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes; 
    currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds; 

    // Decides whether AM or PM 
    var timeOfDay = (currentHours < 12) ? "AM" : "PM"; 

    // Convert the hours component to 12-hour format if needed 
    currentHours = (currentHours > 12) ? currentHours - 12 : currentHours; 

    // Convert an hours component of "0" to "12" 
    currentHours = (currentHours == 0) ? 12 : currentHours; 

    // Creates the display string 
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay; 

    // Updates the time display 
    document.getElementById("clock").firstChild.nodeValue = currentTimeString; 
} 

// --> 
</script> 
<link rel="stylesheet" type="text/css" href="week9live.css" /> 
</head> 

<body onload="updateClock(); setInterval('updateClock()', 1000)"> 
<h1>A live clock in Javascript</h1> 
<div> 
    <p>The time according to your pc is </p> <span id="clock">&nbsp;</span> 
</div> 
</br> 
<button type ="button" onclick = "clearInterval('updateClock()')">Stop Clock</button> 
<button type="button" onclick="setInterval('updateClock()', 1000)">Start Clock</button> 
</body> 
</html> 

我有這很多我用來生成活動時鐘的HTML。然後我的任務是創建兩個按鈕,一個停止時鐘,然後一秒鐘重新啓動它。我的setInterval函數工作正常,但我不能爲我的生活找出爲什麼clearInterval函數不起作用。有任何想法嗎?問題與我的clearInterval函數

乾杯

+3

'clearInterval'預計到'setInterval'返回的ID。總是值得閱讀一些文檔,而不是假設事情是如何工作的:https://developer.mozilla.org/en/DOM/window.clearInterval – 2011-12-29 15:50:20

回答

3

您需要保存這setInterval返回的價值,並通過到您的來電clearInterval

var idForClear = setInterval(updateClock, 1000); 

然後

clearInterval(idForClear); 

如果溝內聯DOM 0級處理器和做這樣的事情

<button id="stopBtn" type ="button">Stop Clock</button> 
<button id="startBtn" type="button">Start Clock</button> 

var idForClear; 
document.getElementById("startBtn").onclick = function() { 
    idForClear = setInterval(updateClock, 1000); 
}; 
document.getElementById("stopBtn").onclick = function() { 
    clearInterval(idForClear); 
}; 

只要確保你把這個腳本這將是一個容易得多在您頁面正文的底部處,確保document.getElementById在您準備好dom之前不會被調用。


而且,它通常被認爲是邪惡的調用setTimeoutsetInterval一個字符串。如果你不是一個內嵌處理器,而不是此

setInterval('updateClock()', 1000); 

你想要在功能

setInterval(function() { updateClock(); }, 1000); 

但當然updateClock的傳遞是一個函數,也使以上是更嘈雜相當於

setInterval(updateClock, 1000); 
+0

感謝幫助隊友。我已將您提供的代碼添加到頁面正文的底部,但該按鈕仍然不起作用。我的網頁上的哪些代碼需要刪除或更改?當談到Javascript時,它並不是最熱門的:P – user1121477 2011-12-29 16:30:18

+0

@ user1121477 - 我將通過啓動和停止按鈕執行一個setInterval來開始調試,該setInterval只是執行一個console.log - 然後檢查它的chrome控制檯是否正常工作。然後慢慢加入你的所有時鐘的東西。 – 2011-12-29 16:32:28

0

clearInterval該函數應該接收由setInterval產生的ID。喜歡的東西:在clearInterval 的setInterval的

var theid = setInterval('updateClock()', 1000); 
clearInterval(theid);