How does js execute Code on Synchronize

Page Code:

<div id="faqdiv" style="display:none;">
    <h1>,!</h1>
    <h3><span id="successName" style="color: orangered"></span>,<a></a>...</h3>
    <h4><span id="time">10</span></h4>
    <h5><a href="/">| ...</a></h5>
    <div id="loader">
        <div id="top"></div>
        <div id="bottom"></div>
        <div id="line"></div>
    </div>
</div>

js Code:

setTimeout(function(){
    window.location.href="/";
},10000);//10
after();

var i=10;
//
function after(){
    $("-sharptime").css("color","red").empty().append(i);
    i=i-1;
    setTimeout(function(){
        after();
    },1000);
}
$("input[name="userName"]").siblings(".form_hint").hide();
$("-sharpfaqbg").css({display:"block",height:$(document).height()});
$("-sharpsuccessName").text($("input[name="userName"]").val());
$("-sharpfaqdiv").css("top","280px");
$("-sharpfaqdiv").css("display","block");
document.documentElement.scrollTop=0;

the effect of this code is to pop up a box with a countdown effect, but because the code is executed from top to bottom, there will be a delay in seconds of the countdown

clipboard.png

clipboard.png
is empty for a while before it appears, and then jumps to the page in 1 second, without waiting for 0 seconds

Mar.11,2021

it doesn't matter if this Synchronize is different, it's simply because you didn't design the countdown well--

  

javascript itself is a Synchronize mechanism, and the task is a stack queue, which will not continue to execute until the current task is completed. SetTimeout is one of the asynchronous mechanisms. Also, the time of the timer is affected by the execution of the current task and the response of the page. Well, after a lot of talk, it's easy to solve this problem. Write a countdown directly. When it's equal to zero, the execution mechanism. The general idea is setTimeOut (fn,time), time is a variable. When the external condition reaches a certain value, change it to 0; or other method calls are fine.

Menu