JavaScript,如何设置警报,它将在倒计时结束时出现

JavaScript,如何设置警报,它将在倒计时结束时出现,javascript,Javascript,我应该更改或添加什么?当倒计时在0结束时,警报将出现。 现在,当倒计时在1结束时,将显示警报 <script> function fn() { var seconds = document.getElementById("num").value; document.getElementById("test1").innerHTML=seconds; var

我应该更改或添加什么?当倒计时在0结束时,警报将出现。 现在,当倒计时在1结束时,将显示警报

    <script>
        function fn() {

        var seconds = document.getElementById("num").value;
        document.getElementById("test1").innerHTML=seconds;
      
        var countdown = setInterval(function() {
            seconds--;
            document.getElementById("test1").innerHTML=seconds;
            if (seconds <= 0) clearInterval(countdown);
            if (seconds == 0) alert("Time up!!");
        },1000);
       
        
        
        }
    

    </script>

    <p>Enter a number from 1 to 100: <br>
        <input id="num" type="text" name="number"></input>
    </p>

    <button type="button" onclick="fn()">Click me</button><br><br>

    <span id="test1"></span>

函数fn(){
var seconds=document.getElementById(“num”).value;
document.getElementById(“test1”).innerHTML=seconds;
var countdown=setInterval(函数(){
秒--;
document.getElementById(“test1”).innerHTML=seconds;

如果(秒实际上
alert
阻塞了所有事件循环和UI。这就是为什么其他事情必须等待
alert
完成它的工作

您可以使用
setTimeout(()=>alert('Times Up!'))
来解决您的问题


函数fn(){
var seconds=document.getElementById(“num”).value;
document.getElementById(“test1”).innerHTML=seconds;
var countdown=setInterval(函数(){
秒--;
document.getElementById(“test1”).innerHTML=seconds;
如果(秒警报('Times Up!'));
},1000);
}
输入1到100之间的数字:

单击我