简体   繁体   English

jQuery javascript防止连续输入更改事件触发器

[英]jQuery javascript prevent continuous input change event triggers

I have come accros this problem before. 我以前来解决这个问题。 This time it is an HTML5 color input giving the problem. 这次是HTML5颜色输入出现问题。 Here's the issue. 这是问题。 I want to use an onChange event to send the user selected value from a color input to the server via Ajax - for an almost real time update to a database. 我想使用onChange事件将用户选择的值从颜色输入通过Ajax发送到服务器-几乎实时更新到数据库。 However, when the user drags his mouse over the color swatch, it will trigger loads of onChange events in rapid succession, for each value change, so the server will get inundated with hundreds of pointless requests until the user stops sliding their mouse! 但是,当用户将鼠标拖动到色样上时,它将每次连续触发onChange事件,对于每个值更改,因此服务器将被成百上千的毫无意义的请求淹没,直到用户停止滑动鼠标!

I could use onMouseOut for this particular issue, but I have had this issue with other things like detecting browser window resizing when a user click/drags to resize and the event is triggered with every pixel change. 我可以将onMouseOut用于此特定问题,但是我在其他方面遇到了此问题,例如当用户单击/拖动以调整大小并且每次像素改变都会触发事件时,检测浏览器窗口的大小。 So I'm looking for an answer that will trigger on the final value only, for all similar scenarios. 因此,对于所有类似的情况,我正在寻找一个仅在最终值上触发的答案。

What is the best method for dealing with this and just triggering when the data has finished changing. 什么是处理此问题并在数据完成更改时触发的最佳方法? Ie ignore every change until user has settled on a final value. 即忽略任何更改,直到用户确定了最终值。 An onAfterChange method event would be nice, but I can't find one! 一个onAfterChange方法事件会很好,但是我找不到!

You can prevent this by debouncing your event handler function. 您可以通过反跳事件处理程序功能来防止此情况。

Every time a debounced function is called, it "pushes back" its execution by n ms. 每次调用去抖动功能时,它将“执行”的时间“推回” n ms。 This means that while triggered numerous times within t ms (eg: while the mouse is moving), it will only execute the handler after not being triggered for t ms (eg: when the mouse has stopped moving). 这意味着,当触发大量的内多次t毫秒(例如:当鼠标移动时),它只会被触发后执行的处理程序t MS(例如:当鼠标停止移动)。

Edit: 编辑:

Ok, the jquery link was a bit out-of-date.. a better example of how to debounce can be found in the Underscore.js source here . 好的,jQuery链接有点过时了。有关如何去抖动的一个更好的示例可以在Underscore.js源代码中找到

How about this: 这个怎么样:

  1. Set a timeout to send request to server, after the first onChange triggering, and change the first flag to false . 在第一个onChange触发后,设置timeout以将请求发送到服务器,然后将第一个标志更改为false

  2. Every onChange is triggered, reset the timeout count. 每次触发onChange ,请重置超时计数。

  3. After sending request, reset the first flag. 发送请求后,重置第一个标志。

Edit: This is the sample code: 编辑:这是示例代码:

 $(document).ready(function(){ var myInterval = undefined; var mytimeoutcount = NaN; var i =0; // for debug $("#myinput").keyup(function(){ console.log("event trigger"); // for debug mytimeoutcount = 300; if(myInterval == undefined){ myInterval = setInterval(function(){ mytimeoutcount -= 10; if(mytimeoutcount <= 0){ // do request, for debug i+=1; console.log(i); window.clearInterval(myInterval); myInterval = undefined; } }, 10); } }); }); 
 #myinput{ width:100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="myinput" type="text" value="test" /> 

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

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