Webolution Designs

Timers

Share This Post

Timers are used in JavaScript for things like animation, firing functions, etc. They run asynchronously so you don’t have to wait on them. Here is an example…

let timeoutFn = setTimeout(function(){     console.log(‘1 second timer’); }, 1000); 

The setTimeout function fires code when we tell it to based upon the time we give it. In the above example, we are running a function that outputs a console log message after one (1) second. The time is the 1000 number. It is in milliseconds.

Notice that we are storing the function in a variable. This allows us to cancel the repeated code with the clearTimeout function as follows…

clearTimeout(timeoutFn); 

We can also set a function to run repeatedly with setInterval…

let intervalFn = setInterval(function(){
    console.log(‘1 second timer’);
}, 1000); 

This will run until we tell it to stop. We do that with clearInterval…

clearInterval (intervalFn);

Happy Coding!

Clay Hess

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Code

Curriculum Innovation or Confusion

It is an early Monday morning just prior to the beginning of the semester.  A faculty member walks into the institution where he teaches.  He

Code

Creating a Promise

Promises are used for asynchronous code. It is a temporary holder for a value that gets returned by an asynchronous call. For example, a call

Do You Want To Boost Your Business?

drop us a line and keep in touch

Excuse Our Mess! We are currently remodeling!

We are currently in the process of redesigning our site. We appreciate your patience as some areas of our site show chipping and paint peeling. We hope to have it sparkling soon!

small_c_popup.png

Learn how we helped 100 top brands gain success.

Let's have a chat