Question

I'm learing javascript. But i have a problem when i use setInterval function with alert function.
I use SetInterval to run a function every one second but when i call alert, the function run by setInterval is stopped. This is my code. How I can fix it?

<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>
Was it helpful?

Solution

When the alert() function is called, JS process is interupted by the browser. Therefor you shold use a html-dialog, like @dystroy suggested.

I prefer the jQuery UI Dialog, which is easy to implement. You can define callbacks for buttons, title of the dialog, etc. observe ;)

Here the Link to the API documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top