Why does this code output 0? I feel like setInterval () has been skipped. I'm so confused. Solve

after this code is executed, the console outputs 0 directly without first executing the anonymous function
in setInterval (). The code is as follows:

        var div = document.getElementById("div");
        div.onclick = function() {
            var i = 0;
            var timer = null;
            timer = setInterval(function() {
                iPP;
                console.log(i)
                if(i == 10) {
                    clearInterval(timer);
                }
            },300);
            if(i == 0) {
                clearInterval(timer);
                console.log(i);
            }
        }

clipboard.png
is inexplicable! Ask the great god to solve the doubt

May.15,2021

the delay operation is added to the task / asynchronous queue and is performed when the main line is finished.


you set the timing function to execute in 300ms. Do you feel that walking this section will be slower than 300ms? It must be output first and then wait for the timer to trigger


asynchronously understand


Why do you think the code in settimeout should be executed first?

Menu