简体   繁体   English

jQuery / JavaScript:中断事件?

[英]jQuery/JavaScript: interrupt event?

This is a really basic JavaScript question and probably duplicate, but I don't know the answer! 这是一个非常基本的JavaScript问题,可能重复了,但是我不知道答案!

I have code as follows: 我的代码如下:

function userlist_change(myval, function_type) {
   // relatively slow code involving Ajax call  
   // based on Ajax results, change some client-side stuff
}
$("#subjectlist").change(function() { 
    userlist_change($("#subjectlist").val(), 'change');
}).change(); 
$("#subjectlist").keypress(function() { 
    userlist_change($("#subjectlist").val(), 'keypress');
}); 

I have the problem that if the .change() event is called, the userlist_change function kicks off, and it's relatively slow. 我有一个问题,如果.change()事件,则userlist_change函数开始,并且它相对较慢。 If the user changes the list again (eg by typing), my code waits for userlist_change to complete before restarting it with the new value. 如果用户再次更改列表(例如,通过键入),我的代码将等待userlist_change完成,然后使用新值重新启动它。

This looks quite odd in the UI, as it can take a few seconds for anything to change client-side - and sometimes the results of the first call only appear after the user has already made a second call. 在用户界面中,这看起来很奇怪,因为更改客户端的内容可能要花费几秒钟的时间-有时,第一次调用的结果只会在用户已经进行第二次调用之后才会出现。

Is there any way I can interrupt any existing userlist_change process when the .change() or `keypress() event is fired? .change()或` .change() ()事件时,有什么方法可以中断任何现有的userlist_change进程吗?

[EDIT] What would be ideal is a simple 'kill any running functions with this name' command - is this possible? [编辑]理想的情况是简单的“使用此名称杀死所有正在运行的功能”命令-这可能吗? Or do I really have to fiddle around with timers?! 还是我真的必须摆弄计时器?!

you can store last request time in a global variable, and store a request time in each ajax request, so that when you are just showing the result of first request, if the global last request time is greater than request, request time, you should show, other wise not. 您可以将最后一个请求时间存储在一个全局变量中,并在每个ajax请求中存储一个请求时间,这样,当您仅显示第一个请求的结果时,如果全局最后一个请求时间大于请求的请求时间,则应该显示,其他明智的不。 For example: 例如:

var lastRequestTime;

function userlist_change(myval, function_type,requestTime) {

   // relatively slow code involving Ajax call  
   // based on Ajax results, change some client-side stuff

if(lastRequestTime <= requestTime){
 //show
}
}
$("#subjectlist").change(function() { 
    lastRequestTime = new Date();
    userlist_change($("#subjectlist").val(), 'change',lastRequestTime );
}).change(); 
$("#subjectlist").keypress(function() { 
    lastRequestTime = new Date(); 
    userlist_change($("#subjectlist").val(), 'keypress',lastRequestTime );
}); 

You should use throttling of event. 您应该使用事件限制。 It is quite easily done with RX for JavaScript, but library is quite complicated. 使用RX for JavaScript可以很容易地做到这一点,但是库却相当复杂。 You can try filter value with timer. 您可以尝试使用计时器过滤值。

Here is useful plugin for throttling: http://benalman.com/projects/jquery-throttle-debounce-plugin/ 这是节流的有用插件: http : //benalman.com/projects/jquery-throttle-debounce-plugin/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM