Throttling
When a function needs to be called frequently, it will consume a lot of resources, use of a throttle way to reduce the consumption of resources
That is within the specified time so that only function is triggered for the first time to take effect, the latter does not take effect.
// Fn is a function to be executed, dalay the delay time required for the function Throttle (Fn, Delay) { // time of the recording trigger a function var lastTime = 0 ; // use closures can be prevented so that the value of each lastTime are initialized invocation is 0 return function () { var nowtime = Data.now (); IF (nowtime-lastTime> Delay) { // Fixed point of this problem fn.call ( this ); // time synchronization lastTime = nowtime; } } }
Shake
A function frequently trigger within the specified time of need, just let the last time to take effect, the front does not take effect
function Debounce (Fn, Delay) { // recording time delay var timer = null // first set is empty return function () { // use a closure to prevent timer is initialized to null each call; the clearTimeout (Timer) // time delay Clear // reset the new delay Timer = the setTimeout ( function () { // set delay fn.apply ( this ); // correction function fn this pointing }, Delay) } }
a function of the call () and apply () method
call and apply methods are Function prototype object, which is to function as a specific method to bind to the specified object calls.
And functions in the same manner apply and call usage parameter difference is how they are transmitted, wherein the apply () is passed the array parameters embodiment, the method of call parameters passed numerical embodiment.
And call () method can accept a plurality of argument lists, Apply () method can only receive or pseudo-array-based array, the array elements will be passed to the called function as parameters.