1秒钟后调用JavaScript函数

BLK*_*zon 12 javascript setinterval

我已成功设法使用setInterval函数在400毫秒后点击div.我的问题是它不断运行,我只需要执行一次的函数.快速搜索后,我发现clearInterval可以停止setInterval.我错误地使用了这个吗?closeAnimation函数正在点击执行.我在此页面上的代码之后对我的代码进行了建模:http://www.w3schools.com/jsref/met_win_setinterval.asp

function closeAnimation() {
    setInterval(function(){hide()}, 400);
    clearInterval(stopAnimation);
}

var stopAnimation = setInterval({hide()}, 400); 
Run Code Online (Sandbox Code Playgroud)

Car*_*ham 31

如果它只需要运行一次就可以使用 setTimeout

setTimeout(function () {
  //do something once
}, 1000);
Run Code Online (Sandbox Code Playgroud)


bet*_*nzi 5

您应该使用setTimeout():

setTimeout(function() {
    getScore(); 
    getResult(); 
}, 1800000);
Run Code Online (Sandbox Code Playgroud)

“ 1800000”是您希望此函数执行之后的时间(以毫秒为单位)。在这种情况下,需要30分钟。