Show message while javascript is running

Stefano Radaelli

I know that most probably such question has been requested several times but I have a need to show a popup or message or whatelse when a javascript function is triggered and hide it at the end.

See this simple js fiddle

That's the HTML code:

<div id='message'>Default message</div>
<input type='button' onclick='Run();' value='start'>

the div 'message' has to contain a simple text like "running" or "please wait" or ...

That's the JS function that took (delay) 3 seconds:

function Run() {
    var delay = 3000;   //-- 3 seconds
    $( '#message' ).text('Please wait ' + delay + ' seconds...');
    var start = new Date().getTime();
    while (true) {
        current = new Date().getTime();
        if ( (start + delay) < current) {
            break;
        }
    }
    $( '#message' ).text('Done');
}

The expected behaviour is that the '#message' div contains "Please wait 3 seconds..." before to enter in the loop and "Done" string only at the end.

But that's not. Can anyone explain me (most probably "again") why or suggest a link where to find an answer?

Quentin

The JS event loop is too busy running while (true) {} to handle a DOM repaint.

Use setInterval, setTimeout, or requestAnimationFrame to trigger the next test of the time instead of a loop.

function Run() {
    var delay = 3000;   //-- 3 seconds
    $( '#message' ).text('Please wait ' + delay + ' seconds...');
    var start = new Date().getTime();
    setTimeout(whenDelayIsOver, 50);

    function whenDelayIsOver() {
        if ( (start + delay) < current) {
            $( '#message' ).text('Done');
        } else {
            setTimeout(whenDelayIsOver, 50);
        }
    }
}

… or just use setTimeout in the first place.

function Run() {
    var delay = 3000;   //-- 3 seconds
    $( '#message' ).text('Please wait ' + delay + ' seconds...');
    setTimeout(whenDelayIsOver, delay);

    function whenDelayIsOver() {
        $( '#message' ).text('Done');
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Show message while javascript is running

From Dev

while controller action running, show user message

From Dev

JavaScript Worker : how to check if a message was received while running expensive task

From Dev

Scrolling while message box is running

From Dev

Javascript redirect then show a message

From Dev

Does browser show HTML changes or allow submitting form while JavaScript is still running?

From Dev

Give the message to the user while running the job

From Dev

Using Javascript to show and hide a message

From Dev

jQuery - Show loading message while using replaceWith

From Dev

jquery mobile autocomplete , to show message while searching

From Dev

Show a Ubuntu Icon while running a Python script

From Dev

Thread for show pics while a function is running

From Dev

How to get ProgressBar to show while OnClickListener is running?

From Dev

How to detect browser and show message without javascript?

From Dev

Javascript redirect inside PHP does not show the message

From Dev

Javascript show message and hide message onclick on select tage

From Dev

nodejs give unwanted message while running shell script in linux

From Dev

Error while running batch scripts 'Error message highlight pattern:'

From Dev

I want to show an error message while entering data itself (on keyup)

From Dev

Python: Show message 'Waiting for player...' while socket listens for connection

From Dev

Check for internet on all activity and show message WHILE net is disconnected

From Dev

show confirmation message while removing item from cart woocommerce

From Dev

Show message is user is in active mode while login laravel

From Dev

Firefox: No click events while JavaScript is running

From Dev

How to show 'in progress' while a FileResult function is running in MVC4

From Dev

How to show a busy indicator while a long process is running?

From Dev

Android - how to show progress dialog while service is running?

From Dev

How can I show an icon in the panel if (and while) a certain process is running?

From Dev

How to show a busy indicator while a long process is running?

Related Related

  1. 1

    Show message while javascript is running

  2. 2

    while controller action running, show user message

  3. 3

    JavaScript Worker : how to check if a message was received while running expensive task

  4. 4

    Scrolling while message box is running

  5. 5

    Javascript redirect then show a message

  6. 6

    Does browser show HTML changes or allow submitting form while JavaScript is still running?

  7. 7

    Give the message to the user while running the job

  8. 8

    Using Javascript to show and hide a message

  9. 9

    jQuery - Show loading message while using replaceWith

  10. 10

    jquery mobile autocomplete , to show message while searching

  11. 11

    Show a Ubuntu Icon while running a Python script

  12. 12

    Thread for show pics while a function is running

  13. 13

    How to get ProgressBar to show while OnClickListener is running?

  14. 14

    How to detect browser and show message without javascript?

  15. 15

    Javascript redirect inside PHP does not show the message

  16. 16

    Javascript show message and hide message onclick on select tage

  17. 17

    nodejs give unwanted message while running shell script in linux

  18. 18

    Error while running batch scripts 'Error message highlight pattern:'

  19. 19

    I want to show an error message while entering data itself (on keyup)

  20. 20

    Python: Show message 'Waiting for player...' while socket listens for connection

  21. 21

    Check for internet on all activity and show message WHILE net is disconnected

  22. 22

    show confirmation message while removing item from cart woocommerce

  23. 23

    Show message is user is in active mode while login laravel

  24. 24

    Firefox: No click events while JavaScript is running

  25. 25

    How to show 'in progress' while a FileResult function is running in MVC4

  26. 26

    How to show a busy indicator while a long process is running?

  27. 27

    Android - how to show progress dialog while service is running?

  28. 28

    How can I show an icon in the panel if (and while) a certain process is running?

  29. 29

    How to show a busy indicator while a long process is running?

HotTag

Archive