Optimizing the performance of the front end of the throttle teacher Shiqianfeng

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)

  

  

 

Guess you like

Origin www.cnblogs.com/xiaojianwei/p/10986064.html