What is the execution process of this code?

I know that the test event will be triggered after scrolling, but the internal process is not very clear. Why is there a return function in it? Which parameter does arguments represent?

function debounce(func,wait,immediate){
    var timeout;
    return function(){
        var context = this,args = arguments;

        var later = function(){
            timeout = null;
            if(!immediate) func.apply(context,args);
        };
        var callNow = immediate&&!timeout;
        clearTimeout(timeout);
        timeout=setTimeout(later,wait);
        if(callNow) func.apply(context,args);
    };
};

var test = debounce(function(){
  console.log("successful")
},250);

window.addEventListener("scroll",test);

I used debugging tools to debug and found that arguments points to event, please help me tell me how it is executed, thank you!

May.26,2022

1. Because as a detrembling function, how can it be executed multiple times without the return function.
2. what is arguments

The

process is to record a timer (here called timeout ), and if it exists, clear the timer , that is, it will not be repeated for a certain period of time.

Menu