0

I use pure JavaScript no libraries I am newer to client side programming and especially to JavaScript.

I have my client side window delimited to the left and right side.

At some point when user press button completeTask event handler is fired.

The event handler adds two HTML elements one on left side another on right side of window with delay about 3000msec.

Here is conceptual description of event handler:

        function completeTask() 
        {
            addSomeElemntToLeftSide(); 
            delay(3000);
            addSomeElemntToRightSide();
        }

The problem is that both HTML elements have been added to the DOM after event handler is executed without any delay.

Any idea what should I do to make delay between adding two elements left and then after 3000msec right?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Michael
  • 13,950
  • 57
  • 145
  • 288

3 Answers3

3

Javascript does not have a delay() function. For something like this you would want to use setTimeout, like so:

function completeTask() {
    addSomeElemntToLeftSide(); 
    setTimeout(addSomeElemntToRightSide, 3000);
}

The addSomeElemntToRightSide function is passed to setTimeout, which will run it in 3000ms (3 seconds). You could also write it like this:

function completeTask() {
    addSomeElemntToLeftSide(); 
    setTimeout(function () {
        addSomeElemntToRightSide();
    }, 3000);
}
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1

Simple...

function completeTask() 
        {
            setTimeout(addSomeElemntToLeftSide,3000); 

            setTimeout(addSomeElemntToRightSide,3000);
        }

if you only want a delay between the two calls, use the setTimeout only in second call...

DZanella
  • 243
  • 1
  • 8
1

You could use setTimeout():

setTimeout(addSomeElemntToRightSide, 3000);

The second parameter is the time you wish to wait in milliseconds.

user2121620
  • 678
  • 12
  • 28