简体   繁体   English

单击按钮时停止页面重新加载

[英]stopping page reload when button is clicked

I am trying to reload a page with a button in it. 我正在尝试重新加载带有按钮的页面。 When the user click on the button an alert window should popup and the page reload should stop until ok is clicked on the alert window. 当用户单击按钮时,将弹出一个警报窗口,并且页面重新加载应停止,直到在警报窗口上单击“确定”为止。

I am writing my code as follows: 我正在编写我的代码,如下所示:

<button id="myButton" onclick="myfunction()"> Test </button>

<script type="text/javascript">

 function myfunction()
  {
    if ( document.getElementById("myButton").onclick == false )
     {
        window.setTimeout(function(){ document.location.reload(true); }, 1000);
     }
    else
    {
       alert ( "Timer Not Set." );
    }
  }
</script>

Neither the if or else conditions are working. if或else条件都不起作用。

Use setInterval and then clearInterval whenever you want to pause or stop, and then reassign setInterval to resume. 要暂停或停止时,请使用setInterval,然后使用clearInterval,然后重新分配setInterval以继续。

Something on the lines of this: 与此类似的东西:

var timer = window.setInterval(reloader, 3000);

$("#btn").on("click", function() {
    clearInterval(timer);
    alert("Just click ok");
    timer = window.setInterval(reloader, 3000);
});

function reloader() {
    document.location.reload(true);
}

Demo : http://jsfiddle.net/abhitalks/79v0xfjd/1/ 演示http : //jsfiddle.net/abhitalks/79v0xfjd/1/

.

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

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