Delaying execution with setTimeout

JavaScript timeout function

setTimeout(fun, 3600);

but what if I don't want to run any other function. Can I do setTimeout(3600);?

+3
source share
3 answers

I'm not sure what you are trying to do. If you want nothing to happen after a period of time, why do you need it first setTimeout()?

+6
source

Based on what you are saying, you are simply trying to defer execution inside a function.

Say, for example, you want to trigger an alert, and after 2 seconds, a second warning:

alert("Hello")
sleep
alert("World")

In javascript, the only 100% way to accomplish this is to split the function.

function a()
{
alert("Hello")
setTimeout("b()",3000);
}
function b()
{
alert("World");
}

setTimeout

function a()
{
  alert("Hello");
  setTimeout(function() {
    alert("World");
  },3000);
}
+10

You can always pass a handler that does nothing:

setTimeout(function() { }, 3600);

But I can hardly imagine any scenario in which this would be useful.

+2
source

Source: https://habr.com/ru/post/1764748/


All Articles