Sponsor

Hold on a second! sleep(), wait() or delay() functionality using JavaScript

How to implement sleep(), wait() or delay() functionality or behavior using JavaScript?

JavaScript is a (browser side) client side scripting language where we could implement client side validation and other features. There needs to implement sleep(), wait() or delay() functionality or behavior with JavaScript like PHP or other languages support. So, how would you like to do that!

JavaScript does not have above functions but it has Date(), setTimeout() or setInterval() functions. There are different ways to do that feature by using these functions. Follow the given steps and/or pseudo code and go through the sample script to achieve it.

Method : With Date() function.
    var now = new Date().getTime();
    var millisecondsToWait = 1000; /* i.e. 1 second */
    Using while loop:

    while ( new Date().getTime() < now + millisecondsToWait )
    {
        /* do nothing; this will exit once it reached the time limit */
        /* if you want you could do something and exit*/
        /* mostly I prefer to use this */
    }
    Using for loop:

    for ( var i = 0; i < 1e7; i++ )
    {
        if ( ( new Date().getTime() - now ) > millisecondsToWait )
        {
            break;
            /* do nothing; this will exit once it reached the time limit */
            /* if you want you could do something and exit */
        }
    }
    Example:
    function sleep( millisecondsToWait )
    {
        var now = new Date().getTime();
        while ( new Date().getTime() < now + millisecondsToWait )
        {
            /* do nothing; this will exit once it reaches the time limit */
            /* if you want you could do something and exit */
        }
    }
    Usage:
    console.log( 'Hey, wait a second!' );
    sleep( 1000 );
    console.log( 'Oh, come on!' );
Method : With setTimeout() function. This will execute a function after waiting a specified period of milliseconds or seconds. The syntax is as given below.
    setTimeout( callbackFunction, millisecondsToWait );
    setTimeout( function() {
        console.log( 'Hey, wait a second!' );
        /* do something or nothing; this waits for a second because 1000 milliseconds have been passed */
    }, 1000);
    Example:
    function holdOn( millisecondsToWait )
    {
        setTimeout( function() {
            console.log( 'Dude, hold on for a minute!' );
            /* do something or nothing; this waits for a minute because 60000 milliseconds have been passed */
        }, millisecondsToWait );
    }
    Usage:
    console.log( 'Dude, hold on for a minute!' );
    holdOn( 60000 );
    console.log( 'Hey, it\'s already late, I\'m gonna sleep now.' );

Now you came to know how to implement sleep() or wait() functionality in JavaScript. setTimeout() function will also supports closure() concept of calling functions inside the function. I will explain what are setTimeout(), setInterval() and closure() in another article.

Happy Coding!

Post a Comment

2 Comments

  1. function wait( seconds ) {
    millis = seconds*1000 ;
    for( var ini = new Date().getTime() ; (new Date().getTime() - ini) < millis ; ) ;
    }

    ReplyDelete

Search This Blog