Ale*_*man 2 javascript setinterval
我希望只有在单击按钮时才会调用alertMe函数,但是它会被调用(调用setInterval调用它),就像页面加载一样.但是,如果我取走pageLoad函数,那么startButton为null,我无法用它做任何事情.谢谢你,伙计们!
/when user clicks start, start the animation
window.onload = pageLoad;
function pageLoad() {
var startButton = document.getElementById("start");
startButton.onclick = setInterval(alertMe,200);
}
function alertMe() {
alert("hi");
}
Run Code Online (Sandbox Code Playgroud)
function pageLoad() {
var startButton = document.getElementById("start");
startButton.onclick = alertMe;
}
function alertMe() {
setInterval(function(){
alert("hi");
},200);
}
Run Code Online (Sandbox Code Playgroud)
在alertMe函数内移动您的间隔,并将其作为参考传递给startButton.onclick
| 归档时间: |
|
| 查看次数: |
17798 次 |
| 最近记录: |