jquery setTimeout or setInterval

后端 未结 3 1123
暗喜
暗喜 2021-01-16 17:28

I have the below code with the if condition

if(oldMembership++ <= newMembership) {
    var digit;
    $(\'ul#indexSiteCounterBottom\').empty();

    for(i         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 18:27

    I think this can resolve your problem...

    function execute_if_membership(){
        setTimeout(function(){
            var digit;
            $('ul#indexSiteCounterBottom').empty();
    
            for(i = 0; i < 9; i++) {
                if(membership.toString()[i] == '_') {
                    digit = ' ';
                } else {
                    digit = membership.toString()[i];
                }
    
                $('ul#indexSiteCounterBottom').append('
  • '+digit+'
  • '); $('ul#indexSiteCounterBottom li:nth-child(3n)').addClass('extra-margin'); } // Execute again if needed if(oldMembership++ <= newMembership) {execute_if_membership();} else{ /* what to do else? maybe call another function */ } },500); } // execute by the first time if(oldMembership++ <= newMembership) {execute_if_membership();}

     

    EDIT: With this code you call the function by the first time. Function wait 500 ms and execute, in the final of the function, it checks if need to call another time (loop) and if needed it executes again. If you want to execute some code after that, you need to put it inside the ELSE of condition, because if you put another code below, it will be executed without wait. That's because setTimeout and setInterval makes the code asynchronous and continues to execute the code.

提交回复
热议问题