[英]Calling a Javascript function with onClick
谁能告诉我为什么使用此计时器:
function timer() {
var counter = 0,
cDisplay = document.getElementById("counter");
format = function(t) {
var hours = Math.floor(t/360000),
minutes = Math.floor( (t/6000) % 60),
seconds = Math.floor( (t/100) % 60),
ms = (Math.floor(t % 100)/100).toFixed(2);
if (t<6000) {
cDisplay.innerHTML = seconds + ms.substring(1);
} else if (t<360000) {
seconds = (seconds < 10) ? "0" + seconds.toString() : seconds.toString();
cDisplay.innerHTML = minutes + ":" + seconds + ms.substring(1);
} else {
minutes = (minutes < 10) ? "0" + minutes.toString() : minutes.toString();
seconds = (seconds < 10) ? "0" + seconds.toString() : seconds.toString();
cDisplay.innerHTML = hours + ":" + minutes + ":" + seconds + ms.substring(1);
}
};
setInterval(function() {
counter++;
format(counter);
},10);
}
当我单击以下标签时将无法启动:
<label onClick="timer()" id="counter">Start Timer</label>
我可以通过在整个函数周围加上括号并在末尾打个分号来使其自动加载:
但我不知道为什么它不会与onClick =“ timer()”一起加载。 我觉得我很想念我很想念,但是我看不到它
这是工作。
将jsfiddle将脚本包含在onLoad
方式更改为No wrap - in <head>
wrap- No wrap - in <head>
演示: http : //jsfiddle.net/UHjas/3/
基本上, timer()
不在全局范围内(请检查jsfiddle iframe源)
那是因为您在load
事件中定义了函数,所以它仅在该作用域内可用。
No wrap - in <head>
中将设置更改为No wrap - in <head>
以在全局范围内定义函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.