KEYUP(函数())AJAX请求延迟 - jQuery的
我有一个jQuery Ajax请求,我想用文字输入打电话,所以我嵌套它里面的 KEYUP(函数()
。这工作得很好。
I have a jQuery Ajax request, that I want to call with text input, and so I nested it inside keyup(function()
. This works fine.
$("#text_box").keyup(function() {
//AJAX REQUEST
});
但有时这种行为越野车。当我输入一些文字非常快,我得到的结果与原来的输入字省略了一些最后一个字母输入字(可能有些挑剔的浏览器)。我想在没有进行第二次输入活动Ajax请求被发送,我的意思是,如果我输入文字非常快,休息一秒钟(意味着我所做的输入)。我怎样才能做到这一点?
But this behaves buggy sometimes. When I input some text very fast, I am getting results for input word with some last letters of the original input word omitted (may be some fault with browser). I want the ajax request to be sent when there is no input activity for a second, I mean, if I input text very fast and rest for a second (means I made the input). How can I do this?
听起来好像你从一个previous Ajax调用的结果。使用定时器的setTimeout
和 clearTimeout
。
It sounds as if you get results from a previous ajax call. Use a timer with setTimeout
and clearTimeout
.
var timer = null;
$("#text_box").keyup(function() {
if(timer) {
clearTimeout(timer);
}
timer = setTimeout(someFunction, someDelay);
});
其中, someFunction
是一个函数,它确实你的Ajax调用和 someDelay
是想要做之前要等待的延迟通话时,用户之后输入,以毫秒为单位。
Where someFunction
is a function which does your ajax call and someDelay
is the delay you want to wait before doing the call, after the user has typed, in ms.