How to solve the problem of function termination in js

<input type="file" onchange="do_css();">



var number = 1;



function do_css(){

var i= 1;

     setInterval(function(){
     
        $(".div_"+number).css({"width":i+"%"})
     i = i+1;
     
     },1000)
      
    
    
    number+1;
    
}

what you want to achieve is that input starts to animate after triggering change (as shown in the code, each trigger is animated by a different element)

but later found a problem: the animation effect starts after the first event is triggered. If the second event is triggered immediately at this time, the animation of the last event will stop immediately, and only the later event will proceed normally. What"s going on, such as the above code reorganization, how should it be written?

Mar.19,2021
The number+1 behind

should be changed to number+=1, brother. The interval call is executed last. The function first adds 1 and then the number in the timer changes with your external value. This is a problem I found. I hope it works for you.


I don't think you should use a timer to do this. What you want to do is probably to do a progress bar that simulates multithreaded uploads of files. If you have to use setInterval to do it, then I think it can be done, really, baffled me, thought about it for a while.

    var number = 1;
    var tmp;
    var loader=[];
    function do_css() {
        clearInterval(tmp);
        var i = 1;
        loader.push(i);
        $(".outdiv").append('<div class="box"></div>');
        tmp = setInterval(function () {
            for (let j = 0; j < $(".box").length; jPP) {
                $($(".outdiv .box")[j]).css({
                    'width': loader[j] + '%'
                })
                loader[j]PP;
            }
            

        }, 1000)
        number += 1;
    }
<input type="file" onchange="do_css();">

    <div class="outdiv">

    </div>

the idea is to use arrays to store the progress of each thread.

https://codepen.io/alexxxcs1/.

Menu