仅在使用jQuery的最后一个键之后进行键控 [英] Keyup only after last key with jquery

查看:70
本文介绍了仅在使用jQuery的最后一个键之后进行键控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的脚本来提取有关输入keyup事件的数据库信息.

I'm coding a simple script to extract database informations on input keyup event.

我的问题是每次用户按下某个键时,总是会重复发生keyup事件.我怎么才能让它只在按下最后一个键后才能起作用?

The problem i have is that the keyup event is always repeated everytime the user press a key. How can i make it working only after the last key pressed?

我认为我应该在每次键入按键后使用setTimeOut()之类的函数,但是我不知道如何...请问您能给我一个简单的例子吗?

I think i should use a function like setTimeOut() after every keyup, But i don't know how... can you make me a simple example please?

对不起,我的英语不好:)

Sorry for my bad english :)

这就是我想要做的:

$("input").keyup(function()
{
    var timer=setTimeout(function() {
    }, 1000);
    if(!timer)
    {
        //do .post ajax request
    }
});

推荐答案

var timer;

$("input").on('keyup', function() {
    clearTimeout(timer);  //clear any running timeout on key up
    timer = setTimeout(function() { //then give it a second to see if the user is finished
        //do .post ajax request //then do the ajax call
    }, 1000);
});

这篇关于仅在使用jQuery的最后一个键之后进行键控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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