In the browser, some events are triggered very frequently, almost within milliseconds. The most typical event is document. onmousemove. Such frequent events often do more harm than good. Today we can see a good solution. Here we can slightly improve it into a general method. Although it cannot completely solve the problem, it can be effectively mitigated and easy to use.CodeAs follows:
// The FN var onfooendfunc = function (FN) {var delay = 50 that supports parameter passing. // you can adjust the latency based on the actual situation. var executiontimer; return function () {If (!! Executiontimer) {cleartimeout (executiontimer) ;}// your function executiontimer = setTimeout (function () {// alert ('20140901'); FN ();}, delay );};};
The document. onmousemove is used as an example to check the running effect. Before processing with latency, move the mouse and you will find that the event is triggered very frequently. After processing with latency, move the mouse and the event will be less frequently.
<textarea id="TextArea1" name="S1" style="height: 40px; width: 200px; color: #ff0000; clip: rect(auto, auto, auto, auto);"></textarea>
The above is just a document. onmousemove is used as an example. There are many other DOM events with the same problem, such as onscroll, onmousemove, and onresize. Similarly, you can use this method to solve the problem. For details, refer to the example here, methods are essentially the same.