简体   繁体   English

Javascript:倒计时完成时自动单击按钮

[英]Javascript: Automatically clicking a button when countdown is complete

I am trying to display the alert message when the countdown is complete, I am trying the following code but its does not work please help!我正在尝试在倒计时完成时显示警报消息,我正在尝试以下代码,但它不起作用请帮忙!

<!DOCTYPE html>
<html>
<body>

<p id=count></p>

<form>  
  <button id="autoClickBtn" onclick="autoClick()">Click me</button>
</form>

<script>

function autoClick(){alert("I am loaded and automatically clicked");}
        var count = 5;  
            var interval = setInterval(function () {
                document.getElementById('count').innerHTML = count;
                count--;
                if (count === -1) {

                    clearInterval(interval); 
                    window.onload = function () { document.getElementById("autoClickBtn").click() };

                }

            }, 1000 );

</script>


</body>
</html>

If you want to alert once after a certain time.如果您想在一定时间后提醒一次。 Use setTimeout function.使用setTimeout function。 You can add the delay in milliseconds.您可以以毫秒为单位添加延迟。 In the example below I have added a delay of 2 secs.在下面的示例中,我添加了 2 秒的延迟。

setInterval , on the other hand, will run indefinitely again and again after time period defined另一方面, setInterval将在定义的时间段后一次又一次地无限期运行

 setTimeout(function () { window.alert('This is an alert'); }, 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM