jquery - 计时器?

Ian*_*ink 6 jquery

abc()document ready火灾发生5秒后,我需要调用我的功能.

这在jQuery中是否可行?

$(document).ready(function () {

//Wait 5 seconds then call abc();

});

function abc() {}
Run Code Online (Sandbox Code Playgroud)

Rei*_*gel 16

$(document).ready(function () {

//Wait 5 seconds then call abc();
    setTimeout(abc, 5000);

});
Run Code Online (Sandbox Code Playgroud)

setTimeout()链接


Ric*_*kir 8

使用setTimeout("abc();", 5000);你准备好功能.

示例(不要使用此,请参阅下文)

$(document).ready(function () {
  //Wait 5 seconds then call abc();
  setTimeout("abc();", 5000);
});

function abc() {}
Run Code Online (Sandbox Code Playgroud)

5000告诉它等待5000毫秒,这是5秒.这是一个基本的JavaScript函数,不需要jQuery(当然,除了就绪状态事件代码).setInterval()如果您希望重复发生某些事情,也可以使用.

编辑:您可以在此处阅读更多相关信息(已删除链接).
编辑3:虽然我的答案并不正确,但这并不是最好的方法(大卫在下面的评论中引起了我的注意.)更好的方法是将函数abc本身直接传递给setTimeout函数.像这样:

$(document).ready(function () {
  //Wait 5 seconds then call abc();
  setTimeout(abc, 5000);
});

function abc() {}
Run Code Online (Sandbox Code Playgroud)

这是更好的形式,因为你没有传递字符串参数,这eval可能会导致安全风险.

此外,更好的链接,文档是在这里.