-2

I am trying to write some code that displays text, and then removes it two seconds later, however, the setTimeout() method doesn't seem to be working properly for me, the function that is called is running with no delay. Here's the code:

IDtext('IP-saved', "New IP Address saved successfully!");
setTimeout(IDtext('IP-saved', ""), 2000);

function IDtext(ID, text) {
    document.getElementById(ID).innerHTML = text;
}

What am I doing wrong here?

  • You have to pass a reference to a function to `setTimeout()`. Your code passes the result of **calling** the function. – Pointy Nov 27 '20 at 15:31
  • 2
    `setTimeout(IDtext('IP-saved', "")` invokes `IDtext('IP-saved', "")` immediately, you need a function that, when called, invokes IDtext instead – CertainPerformance Nov 27 '20 at 15:31

1 Answers1

2

The correct syntax is

setTimeout(IDtext, 2000, 'IP-saved', '');

the arguments of the referenced function must be placed last

see documentaion of setTimeout()

your syntax way is
var timeoutID = scope.setTimeout(function[, delay, arg1, arg2, ...]);

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40