-1

I am stuck in a situation where I want to put some delay between some lines of javascript code like following :

function sayHello(){
document.getElementById("txt").innerHTML = "Hello";
// some delay of about 2 seconds 
document.getElementById("txt").innerHTML = "I am a programmer";

}

1 Answers1

-3

Try setTimeout("YOUR CODE HERE", 2000 /*(2 seconds in miliseconds)*/) You can read more Here

Friedrich
  • 330
  • 3
  • 10
  • 1
    This question has been asked before – evolutionxbox Mar 09 '22 at 11:28
  • 1
    Passing strings as the first argument to `setTimeout` is (a) inefficient (b) does weird things with scope and (c) makes debugging harder. When using `setTimeout`, pass a function, not a string. – Quentin Mar 09 '22 at 11:29
  • 1
    And W3Schools is awful. In this particular case they aren't so bad that they recommend passing a string, but bad enough that they don't even mention it is possible (which is really bad for an API reference!) – Quentin Mar 09 '22 at 11:29