current position:Home>Solve the problem of binding this point in anti chattering throttling

Solve the problem of binding this point in anti chattering throttling

2022-02-02 20:54:54 CSDN Q & A

The phenomenon and background of the problem
         Why rebind inside this Point to , What's the harm of calling directly without binding / Defects ?
function throttle(fn, delay) {            var lastTime = 0;            return function () {                var nowTime = Date.now();                if (nowTime - lastTime > delay) {                                                fn.call(this);// There are questions here                     lastTime = nowTime;                }            }        }function debounce(fn, delay) {
   
   
   
   // Shake proof             var timer = null;            return function () {                clearTimeout(timer);                timer = setTimeout(function () {                    fn.apply(this);// There are questions here                 }, delay);            }        }
Operation results and error reporting contents
My solution ideas and tried methods
What I want to achieve



Refer to the answer 1:

I think because your anti shake throttling function is modular , It has no global context , Calling... With this function fn when , If you don't rebind fn intra-function this Point to ,fn You can't use it in this 了

If help , Hope to adopt




Refer to the answer 2:



Refer to the answer 3:

It mainly depends on you fn How to write , What does the statement in question really do , Inside this The binding fn, But I didn't do anything , It is triggered when Binding fn What's in , If commented out, can it still run , Think more . Here is an example of anti shake throttling , Is it your own code , If so, there should be no doubt .




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022054516650.html

Random recommended