在最后一次按键5秒后调用javascript函数 [英] Call a javascript function after 5 sec of last key press

查看:156
本文介绍了在最后一次按键5秒后调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有< input type = text /> 的表单,我希望在最后一次按键5秒后调用javascript函数,并且每个按下新键时,此计时器应重置,并且仅在5秒后调用该功能。

I have a form with an <input type=text /> and I want to call a javascript function after 5 seconds of the last key press, and every time a new key is pressed, this timer should reset and only call the function after 5 seconds.

我该怎么做?

我正在使用jQuery。

I'm using jQuery.

谢谢!

推荐答案

这样的事情应该让你开始:

Something like this should get you started:

var timeout;
$('input[type=text]').keypress(function() {
    if(timeout) {
        clearTimeout(timeout);
        timeout = null;
    }

    timeout = setTimeout(myFunction, 5000)
})

这篇关于在最后一次按键5秒后调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆