0
我有一個時鐘應用程序,我可以運行乘法時間,但每次我再次啓動應用程序時,前一個應該停止。我認爲我應該使用目標?這個。但我不知道我應該使用它的功能在哪裏。運行應用程序的乘法時間不停止前面
時鐘的功能。
function myFunction() {
startClock();
function startClock() {
var digits = new Array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
var h = new Date().getHours().toString();
var m = new Date().getMinutes().toString();
var s = new Date().getSeconds().toString();
var t = ((h > 9 ? h : "0" + h) + (m > 9 ? m : "0" + m) + (s > 9 ? s : "0" + s));
hours.appendChild(hour1).className = "clock-digit-" + digits[t.substring(0, 0 + 1)];
hours.appendChild(hour2).className = "clock-digit-" + digits[t.substring(1, 1 + 1)];
minutes.appendChild(minute1).className = "clock-digit-" + digits[t.substring(2, 2 + 1)];
minutes.appendChild(minute2).className = "clock-digit-" + digits[t.substring(3, 3 + 1)];
seconds.appendChild(sec1).className = "clock-digit-" + digits[t.substring(4, 4 + 1)];
seconds.appendChild(sec2).className = "clock-digit-" + digits[t.substring(5, 5 + 1)];
t = setTimeout(function() {
startClock()
}, 1000);
}
}
我創建了一個的jsfiddle http://jsfiddle.net/dymond/nufwb/
但是當你當你按下測試分組兩次通知。時鐘停止...
您正在使用全局變量,因此它們只能引用一組元素。您需要將所有內容收集到一個對象中,並將其作爲參數傳遞給您的函數。 – Barmar 2013-02-12 11:50:13
@Barmar嘿,謝謝。但我真的可以明白你的意思。如果我將變量移動到一個對象中並從該函數中調用該參數,則會得到相同的問題。可能做錯了什麼。 – Dymond 2013-02-12 12:15:20
如果你用局部變量做所有事情,你不會有同樣的問題。函數的每次運行都會創建新的變量,並且它們不會相互衝突。 – Barmar 2013-02-12 12:19:05