Why does js imitate jitter function save this and bind scope for callback when executing

RT because the this pointer in the anonymous function already points to window. I don"t understand what it means if you just want to pass the args parameter list through apply as fn.apply (window, args);.

fn.apply (context, args);


  let timer = null;

  return function() {
    
    let context = this;
    let args = arguments;

    clearTimeout(timer);
    timer = setTimeout(function() {
      fn.apply(context, args);
    }, delay);
  }
} 



Mar.01,2021

this article is very good. I recommend

The this in the

timer points to window,. I suggest you check that the js's this points to


setTimeout. The this in the function points to window. If you need to access the context of function in fn, you need to pass it. Otherwise, it is possible not to pass


the this in the throttled function may not be window


. It is impossible to know the this of the anonymous function from your function definition, depending on how the anonymous function is called.

Menu