>>> JS experts, anyone know how to set timeout in JavaScript?
Printable View
>>> JS experts, anyone know how to set timeout in JavaScript?
simple really:
setTimeout("myFunction()", 1000);
1000 is equal to one second.
example of how setTimeout should be used:
hope this helps you understand it more ;)Code:<html>
<script language="javascript">
var a = 0;
// used as a reference to the timeout so that we
//can stop the loop with clearTimeout
var timeout_handle;
incrementCounter(a);
function incrementCounter(x){
current_loop = x + 1;
alert("loop number" + current_loop);
timeout_handle = setTimeout("incrementCounter(current_loop)", 3000);
}
</script>
<body>
<a href="#" onclick="clearTimeout(timeout_handle)">click here to stop the timeout</a>
</body>
</html>
Thanks.