当事件在 jQuery 中触发一段时间后,我如何执行 jQuery 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5603297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:27:27  来源:igfitidea点击:

how i can execute a jQuery function after a time period when event fire in jQuery?

jqueryeventstextboxonkeyup

提问by Quentin

I want to fire a event on keyup of textbox 300 milisecond later

我想在 300 毫秒后在文本框的 keyup 上触发一个事件

$("#blah").keyup(function () {
               //code is here
            }).delay(300).myfunction();

when i try to execute this function i found error that myfunction is not a function.

当我尝试执行这个函数时,我发现错误是 myfunction 不是一个函数。

so can anyone explain how i can execute a function 300 milisecond later after keyup in a textbox

所以任何人都可以解释我如何在文本框中键入后 300 毫秒后执行函数

回答by Quentin

function myFunction () {
    // Code to do stuff after 300ms
}

$("#blah").keyup(function () {
               // Code to do stuff immediately
               setTimeout(myFunction, 300);
            });

回答by Headshota

myfunction must be defined!

必须定义 myfunction!

$("#blah").keyup(function () {

setTimeout(function(){
            myfunction();
         },300);
})

回答by AmiNadimi

As docssay, from version 1.4 on-wards, you can use delay()function

正如文档所说,从 1.4 版开始,您可以使用delay()函数

$( "#foo" ).slideUp( 300 ).delay( 800 ).fadeIn( 400 );