Javascript pause method: To create delays in javascripts


Creating delays in javascript can be very essential under certain specific scenarios, though i would recommend you to avoid it in most cases.

The right way to create a delay would be by using setTimeOut method in javascript, as below:

setTimeout(“alert(‘hello world’)”,1500); // shows alertbox after 1500 milliseconds

The lazy way or to be precise – the naughty way, can be obtained from the below function:

<html>
<head>
<title>Javascript Pause</title>

<script language=”javascript”>
function pausejs(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}
</script>

</head>
<body>

<form>
               <input type=”button” value=”Push this button to pause the page scripts by 1500 milliseconds” onClick=”pausejs(1500);“>
</form>

</body>
</html>

Of course, this is a naughty equivalent to the pause method. But after all you can have a pause method in your javascript.

One thought on “Javascript pause method: To create delays in javascripts

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s