SetTimeout loop passes in the third parameter, is it really the implementation of the closure?

function fn1 () {

for(var i=0;i<4;iPP){
    var tc=setTimeout(function(i){
        console.log(i);
        clearTimeout(tc)
    },10,i);
}

}
fn1 ()

Why do you say that this examines the understanding of closures?
which part is a closure? The outer part of the
function calls the variables inside the function, but how does the closure here reflect this concept?

Mar.20,2021

closure means clearTimeout (tc) , tc is not in lexical scope.


originally written:

function fn1(){

    for(var i=0;i<4;iPP){
        var tc=setTimeout(function(){
            console.log(i);
            clearTimeout(tc)
        },10);
    }
}
fn1()
I in the

timer callback function is the I of the for loop in the fn1 function . After the fn1 function is executed, the timer callback function still retains a reference to the variable object of the fn1 function .
result: output 4 at one time.

< hr >

the way you write in your question:
I of each for loop is passed to the timer callback function in real time as a parameter, so there is no closure ~

.

Building Master, let me give you a brief overview. Closures are actually secondary functions called by first-level functions. Second-level functions access variables in first-level functions. I like to call this process closure. Of course, there is also a lot of official understanding on the Internet, you can also refer to it, but I feel that this is relatively easy to understand. First of all, the first-level function is fn1, then the second-level function is called with this timeout,

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e52ce1-451df.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e52ce1-451df.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?