我正在学习javascript。但是当我使用具有警报功能的setInterval函数时,我遇到了问题。
我使用SetInterval每秒运行一次函数但是当我调用alert时,setInterval运行的函数停止。这是我的代码。我怎么解决它?
<div id="sec"></div>
<div id="show-alert" onClick="return show()" style="cursor:pointer">Show alert</div>
<script>
var i=0;
setInterval(ascsec,1000);
function ascsec(){
document.getElementById('sec').innerHTML=i++;
}
function show()
{
alert('Hi! This is problem. The sec is not ascending');
}
</script>
答案 0 :(得分:1)
调用alert()
函数时,浏览器会中断JS进程。因此你应该使用一个html对话框,比如@dystroy建议。
我更喜欢jQuery UI Dialog,这很容易实现。您可以定义按钮的回调,对话框的标题等。观察;)
的链接