setTimeout or setInterval not working in JavaScript [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
7 min

banner

# Table of Contents

  1. setTimeout not working in JavaScript
  2. setInterval not working in JavaScript

Note: if you're running into issues when using setInterval(), click on the following subheading:

# setTimeout not working in JavaScript [Solutions]

The setTimeout function might not work in JavaScript if you call it incorrectly, e.g. by passing it the result of calling the callback function.

index.js
// โ›”๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "callback" argument must be of type function. Received undefined setTimeout(callbackFn(), 1000); function callbackFn() { console.log('bobbyhadz.com'); }

settimeout not working

The code for this article is available on GitHub

The issue in the example is that we are calling the callback function with parentheses () in the call to setTimeout.

The setTimeout method expects to get called with a reference to a function and not the result of calling one.

You can resolve the issue by removing the parentheses and passing a reference to a function to setTimeout.

index.js
setTimeout(callbackFn, 1000); function callbackFn() { console.log('bobbyhadz.com'); }

passing reference to function to settimeout

The callbackFn function is invoked after 1 second (1000 milliseconds) in the example.

The setTimeout() method sets a timer that runs a function once the timer expires.

In some cases, you might want to pass one or more arguments to the function.

index.js
setTimeout(() => greet('bobby hadz'), 1000); function greet(name) { console.log(`hello ${name}`); }
The code for this article is available on GitHub

The greet function takes a name argument, so we used an inline arrow function in the call to setTimeout.

This way we are still passing a reference to a function to setTimeout but when the function is invoked, it returns the result of calling greet with the string bobby hadz.

passing parameter to set timeout callback

If you only pass a reference to the greet function to setTimeout, then the name parameter will be undefined.

index.js
// ๐Ÿ‘‡๏ธ hello undefined setTimeout(greet, 1000); function greet(name) { console.log(`hello ${name}`); }

name parameter is undefined

The previous example used an inline arrow function to resolve this, but you can also pass the parameters you want to pass to the callback function directly to setTimeout.

index.js
// ๐Ÿ‘‡๏ธ hello bobby hadz setTimeout(greet, 1000, 'bobby hadz'); function greet(name) { console.log(`hello ${name}`); }

The third argument we passed to setTimeout is passed to the greet function when it gets called after 1 second (1000 milliseconds).

You can use this approach to supply as many parameters as necessary to the callback function.

index.js
// ๐Ÿ‘‡๏ธ hello bobby hadz setTimeout(greet, 1000, 'bobby', 'hadz'); function greet(first, last) { console.log(`hello ${first} ${last}`); }
The code for this article is available on GitHub

The third and fourth arguments we passed to setTimeout get passed to the greet function when it gets called after 1 second.

You can either use an inline arrow function as we did in the previous example or supply the additional parameters after the delay parameter in the call to setTimeout, however, make sure that you aren't calling the callback function with parentheses () in the call to setTimeout.

index.js
// โ›”๏ธ INCORRECT: callback called with parentheses () โ›”๏ธ setTimeout(callbackFn(), 1000); function callbackFn() { console.log('bobbyhadz.com'); }

When you add parentheses to a function, you invoke it immediately and the goal is to invoke a function after N milliseconds.

# Make sure you aren't calling setTimeout with a string instead of a function

Another common cause for setTimeout not to work is when you pass it a string instead of a function.

Here is an example.

index.js
// โ›”๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "callback" argument must be of type function. Received type string ('callbackFn') setTimeout('callbackFn', 1000); function callbackFn() { console.log('bobbyhadz.com'); }

Notice that we passed a string as the first argument to setTimeout.

The method expects to get called with a reference to a function and not a string.

index.js
// โœ… works as expected setTimeout(callbackFn, 1000); function callbackFn() { console.log('bobbyhadz.com'); }
The code for this article is available on GitHub

Passing a reference to a function as the first argument to setTimeout works as expected.

# The delay after which setTimeout is called might not be precise

Here is an example of calling the setTimeout() method that may look confusing at first.

index.js
setTimeout(() => { console.log('First line'); }, 0); console.log('Second line');

Running the code above produces the following output.

shell
Second line First line

set timeout delay is not precise

The code for this article is available on GitHub

The example tries to run a function that logs a message to the console after 0 milliseconds.

One would expect that the First line message would get printed first, however, that's not the case.

When the setTimeout() method is called, it gets added to the event table.

The call stack has to be empty for the event loop to check the event table to see if there are any events waiting in the queue.

Only then will the event loop move the waiting events to the call stack for them to get processed.

The callback function we passed to setTimeout gets queued for execution, however, it is only executed after the call stack is empty and the second console.log() call has run.

# clearTimeout not working in JavaScript [Solved]

If you encounter issues when trying to clear the timeout with the clearTimeout method, make sure to store the timeout ID that is returned from calling setTimeout.

index.js
// ๐Ÿ‘‡๏ธ store timeout ID in variable const timeoutID = setTimeout(callbackFn, 1000); // ๐Ÿ‘‡๏ธ this clears the timeout clearInterval(timeoutID); function callbackFn() { console.log('bobbyhadz.com'); }
The code for this article is available on GitHub

The example clears the timeout immediately.

You will most likely want to clear the timeout if a certain condition is met.

The setTimeout method returns an ID that can be passed to the clearTimeout() method to clear the timeout.

The clearTimeout method cancels a timeout that was previously set up by a call to setTimeout.

If you pass a parameter to the clearTimeout method that is not the ID that is returned from calling setTimeout, then the clearTimeout method does nothing.

# setInterval not working in JavaScript [Solutions]

A common reason for setInterval not to work is when you pass a string to the method instead of passing it a reference to a callback function.

Here is an example.

index.js
// โ›”๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "callback" argument must be of type function. Received type string ('callbackFn') setInterval('callbackFn', 1000); function callbackFn() { console.log('bobbyhadz.com'); }

passing string to set interval

Notice that we passed a string instead of a reference to a callback function to setInterval.

Instead, pass a reference to the function to setInterval.

index.js
// โœ… works as expected setInterval(callbackFn, 1000); function callbackFn() { console.log('bobbyhadz.com'); }

pass reference to function to set interval

The code for this article is available on GitHub

The setInterval method takes a function and a delay in milliseconds and repeatedly calls the function with the specified delay between each call.

# Make sure you aren't calling the function you passed to setInterval

Another common reason for setInterval not to work is if you invoke the callback function you passed to the method with parentheses ().

Here is an example.

index.js
// โ›”๏ธ TypeError [ERR_INVALID_ARG_TYPE]: The "callback" argument must be of type function. Received undefined setInterval(callbackFn(), 1000); // ๐Ÿ‘ˆ๏ธ notice parentheses () function callbackFn() { console.log('bobbyhadz.com'); }

invoking callback function when calling set interval

Notice that we used parentheses to call callbackFn() in the call to the setInterval method.

To resolve the error, remove the parentheses as setInterval expects to get called with a reference to a function and not the result of calling a function.

index.js
// โœ… Works as expected setInterval(callbackFn, 1000); // ๐Ÿ‘ˆ๏ธ removed parentheses function callbackFn() { console.log('bobbyhadz.com'); }

call set interval with reference to function

The code for this article is available on GitHub

If you have to pass arguments to the callback function, use an inline arrow function wrapper.

index.js
// ๐Ÿ‘‡๏ธ hello bobby hadz setInterval(() => greet('bobby hadz'), 1000); function greet(name) { console.log(`hello ${name}`); }

using inline arrow function to pass arguments to callback

The arrow function returns the result of calling greet with the string bobby hadz.

We are still passing a reference to a function to setInterval, so everything works as expected.

You can also pass arguments to the callback function by specifying them after the delay parameter in the call to setInterval.

index.js
setInterval(func, delay, arg0, arg1, ...)

Here is an example.

index.js
// ๐Ÿ‘‡๏ธ hello bobby hadz setInterval(greet, 1000, 'bobby hadz'); function greet(name) { console.log(`hello ${name}`); }

pass argument after delay in set interval call

The argument we passed after the delay of 1000 milliseconds (1 second) gets passed to the greet function after the specified delay.

The same approach can be used to pass multiple arguments to the callback function.

index.js
// ๐Ÿ‘‡๏ธ hello bobby hadz setInterval(greet, 1000, 'bobby', 'hadz'); function greet(first, last) { console.log(`hello ${first} ${last}`); }

The code sample produces the same output however, this time we passed 2 arguments to the callback function (greet).

We used 1000 milliseconds as the delay in the examples.

The delay parameter is used to specify the time, in milliseconds (1000 milliseconds = 1 second), the timer should delay in between executions of the supplied function.

# clearInterval not working in JavaScript [Solved]

If you encounter issues when trying to clear the interval with the clearInterval method, make sure to store the interval ID that is returned from calling setInterval.

index.js
// ๐Ÿ‘‡๏ธ store interval ID in variable const intervalID = setInterval(callbackFn, 1000); // ๐Ÿ‘‡๏ธ this clears the interval clearInterval(intervalID); function callbackFn() { console.log('bobbyhadz.com'); }
The code for this article is available on GitHub

The example clears the interval immediately.

You will most likely want to clear the interval if a certain condition is met.

The setInterval method returns an ID that can be passed to the clearInterval() method to clear the interval.

The clearInterval method cancels a timed, repeating action that was previously set up by a call to setInterval.

If you pass a parameter to the clearInterval method that is not the ID that is returned from calling setInterval, then the clearInterval method does nothing.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev