KEYUP(函数())AJAX请求延迟 - jQuery的 [英] keyup(function()) ajax request delay - jQuery

查看:564
本文介绍了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.

这篇关于KEYUP(函数())AJAX请求延迟 - jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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