Shiqianfeng root teacher craftsman Open forward to explain the expansion and stabilization of the performance optimization of the front end, where the method is throttled.
Throttling: a reduction of the number of events performed within a certain time.
root teacher is resolved through the closure of the throttle problem.
code as follows:
<div class="box"></div>
// function closure is achieved by throttling function throttle (callback, wait) { eat execution time // set the let PREV = 0; // return function return function () { // get the current timestamp let now = Date.now (); // get the parameter ; let args = arguments on // a time with the current time is determined to make the number of time - (prev> wait now) { // If the time stamp on the current time minus the time stamp is greater than the set execution if , executed // this set point callback.apply (this, args); // update execution time PREV = now; } } } // get the elements let document.querySelector Box = ( "Box."); // number of let = 0 CONT; // set event Throttle = box.onmousemove (function () { ++ CONT; console.log ( "Event executed" + cont + "times"); }, 1000)