clearTimeout和setTimeout [英] clearTimeout and setTimeout

查看:128
本文介绍了clearTimeout和setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到一种情况,用户输入搜索查询,并且在输入框的每个键入事件上,我都设置了一个超时(setTimeout)来执行搜索(asp.net脚本服务调用).但是,如果用户仍在输入搜索查询,则必须清除设置的超时时间.
我尝试了一些看似合理的方法,但是没有,没有很多乐趣!

代码:

Hi guys,

I have a situation where a user enters a search query and on each keyup event of the input box i set a timeout (setTimeout) to execute the search(asp.net script service call). But if the user is still typing a search query i have to clear the set time out.
I have tried something that looks reasonable but no no, not much joy!

Code:

var x = '';
$('#search').keyup(function ()
{
    clearTimeout(x);
    x = setTimeout("GetSearchResults($('#search'));", 100);
});



我尝试将超时时间延长为300、400、500,但是执行查询需要很长时间,这是不可取的

关于如何解决此问题或我可以使用的任何其他方法的任何建议?
感谢您的帮助.

祝您编程愉快,
Morgs



I tried increasing the timeout period to say 300, 400, 500 but then the query takes long to execute which is not preferable

Any advise of how i can resolve this or any other approach that i could use?
Will appreciate any help guys.

Happy coding,
Morgs

推荐答案

(' #search').keyup(函数() { clearTimeout(x); x = setTimeout("
('#search').keyup(function () { clearTimeout(x); x = setTimeout("GetSearchResults(


('#search'))); ",
('#search'));", 100); });



我尝试将超时时间延长为300、400、500,但是执行查询需要很长时间,这是不可取的

关于如何解决此问题或我可以使用的任何其他方法的任何建议?
感谢您的帮助.

祝您编程愉快,
Morgs



I tried increasing the timeout period to say 300, 400, 500 but then the query takes long to execute which is not preferable

Any advise of how i can resolve this or any other approach that i could use?
Will appreciate any help guys.

Happy coding,
Morgs


看到您已经在使用JQuery,为什么不使用autocmplete插件;
http://docs.jquery.com/Plugins/autocomplete [
Seeing as you are already using JQuery, why not use the autocmplete plugin;
http://docs.jquery.com/Plugins/autocomplete[^]

could save you some t+e.


这篇关于clearTimeout和setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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