如何在内联onclick事件上设置setTimeout

时间:2014-06-06 19:43:26

标签: javascript html settimeout

基本上我有这个HTML:

<a class="button animated none" 
onclick="document.getElementById('local').scrollIntoView();" href=...

我想在其中添加一个setTimeout到javascript中。尝试了几种方法,但一切似乎都回归了一个未定义的&#39;错误。

编辑:我尝试过的内容:

setTimeout (document.getElementById('local').scrollIntoView(), 2000);
document.getElementById('local').setTimeout (scrollIntoView(), 2000);
document.getElementById('local').scrollIntoView(); setTimeout (scrollIntoView(), 2000);

1 个答案:

答案 0 :(得分:0)

setTimeout的第一个参数是一个函数,但是您发送的执行scrollIntoView()的结果可能是undefined。尝试用匿名函数包装调用:

setTimeout(function() { 
  document.getElementById('local').scrollIntoView() 
}, 2000);