使用jQuery延迟JavaScript函数调用

时间:2021-09-29 05:10:56

JavaScript:

$(document).ready(function(){

    function sample() {
       alert("This is sample function");
    }

    $("#button").click(function(){
        t = setTimeout("sample()",2000);
    });

});

HTML:

HTML:

<input type="button" id="button" value="Call sample function with delay">

Once I click the button, sample() function is not called with a delay of 2 seconds. I don't know what's wrong.

单击该按钮后,不会调用sample()函数,延迟时间为2秒。我不知道出了什么问题。

How to call JavaScript function using setTimeout() via jQuery?

如何使用setTimeout()通过jQuery调用JavaScript函数?

3 个解决方案

#1


80  

Since you declare sample inside the anonymous function you pass to ready, it is scoped to that function.

由于您在传递给ready的匿名函数中声明了sample,因此它的作用域是该函数。

You then pass a string to setTimeout which is evaled after 2 seconds. This takes place outside the current scope, so it can't find the function.

然后将字符串传递给setTimeout,该字符串在2秒后被唤醒。这发生在当前范围之外,因此无法找到该功能。

Only pass functions to setTimeout, using eval is inefficient and hard to debug.

只将函数传递给setTimeout,使用eval效率低且难以调试。

setTimeout(sample,2000)

#2


33  

function sample() {
    alert("This is sample function");
}

$(function() {
    $("#button").click(function() {
        setTimeout(sample, 2000);
    });

});

jsFiddle.

的jsfiddle。

If you want to encapsulate sample() there, wrap the whole thing in a self invoking function (function() { ... })().

如果你想在那里封装sample(),请将整个事物包装在一个自调用函数中(function(){...})()。

#3


13  

Very easy, just call the function within a specific amount of milliseconds using setTimeout()

非常简单,只需使用setTimeout()在特定的毫秒内调用函数

setTimeout(myFunction, 2000)

function myFunction() {
    alert('Was called after 2 seconds');
}

Or you can even initiate the function inside the timeout, like so:

或者你甚至可以在超时内启动函数,如下所示:

setTimeout(function() {
    alert('Was called after 2 seconds');
}, 2000)

#1


80  

Since you declare sample inside the anonymous function you pass to ready, it is scoped to that function.

由于您在传递给ready的匿名函数中声明了sample,因此它的作用域是该函数。

You then pass a string to setTimeout which is evaled after 2 seconds. This takes place outside the current scope, so it can't find the function.

然后将字符串传递给setTimeout,该字符串在2秒后被唤醒。这发生在当前范围之外,因此无法找到该功能。

Only pass functions to setTimeout, using eval is inefficient and hard to debug.

只将函数传递给setTimeout,使用eval效率低且难以调试。

setTimeout(sample,2000)

#2


33  

function sample() {
    alert("This is sample function");
}

$(function() {
    $("#button").click(function() {
        setTimeout(sample, 2000);
    });

});

jsFiddle.

的jsfiddle。

If you want to encapsulate sample() there, wrap the whole thing in a self invoking function (function() { ... })().

如果你想在那里封装sample(),请将整个事物包装在一个自调用函数中(function(){...})()。

#3


13  

Very easy, just call the function within a specific amount of milliseconds using setTimeout()

非常简单,只需使用setTimeout()在特定的毫秒内调用函数

setTimeout(myFunction, 2000)

function myFunction() {
    alert('Was called after 2 seconds');
}

Or you can even initiate the function inside the timeout, like so:

或者你甚至可以在超时内启动函数,如下所示:

setTimeout(function() {
    alert('Was called after 2 seconds');
}, 2000)