Using the setInterval() function in JavaScript

preview_player
Показать описание
You can use the "setInterval()" function in JavaScript to make your code run repeatedly on a timer. This can be super useful for clocks, counters, updating your page with live data or even creating games.

To use this function, you supply it with a "handler function" which will contain your code that you wish to execute repeatedly. You also provide the function with a delay in milliseconds - which actually sets the interval.

In this video I take you through a straightforward example of the "setInterval()" function where we display a number on the page and for every second that passes, that number increases by 1. We also take a look at the "clearInterval()" function which has the ability to cancel a set-interval.

For your reference, check this out:

Support me on Patreon:

Follow me on Twitter @dcode!

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

The sound of your keyboard is just beautiful.

relkovic
Автор

Nice one. Was scratching my head about setInterval but you have given a few pointers. Cheers.

tt
Автор

Interesting, I did not know that clearInterval is keeping track of the numbers passed and it would go further instead of starting from zero again.
And so I thought that you will need to get the number from the timerText and re-assign it to count before continue.

You tutorial is very good. I am sorry that you are not having more likes. You deserve more.

iliashterev
Автор

Amazing! Best tutorial for setInterval I’ve seen! Thanks!

GerardSiebels
Автор

Simple and straightforward code, it solved my problem. Great tutorial!

Автор

Thank u 4 this Video Tutorial. But i have a problem with:
When i click Start or Pause Button second time, i cant started oder paused again. Can u Help me?

AsimYilan
Автор

Hello, i have a problem when i try to run the code.
I can't run the code because he said "Cannot read property 'addEventListener' of null"
can u help me pls?
Thank you.

rafaelribeiro
Автор

Thanks Domb,
But finally I face a new problem, when i pressed double or more click our start button, then this function didn't work properly (rapid counting), ,
so Man whats i can do, , ??🤔

sahadatsajib
Автор

If there are four side signal with two colours red and green. It want to be automatically rotate in clockwise using setinterval and clearinterval when green is on, others want to be red how can I do this. Can any one help me to do this?

padmapriyavks
Автор

Thank you for this video, it helped me to understand that matter.

vaclavvlcek
Автор

But there is a problem. If you click multiple times on the "start timer" it will execute multiple times the function and the numbers start to get counting faster. Even if you click on "Pause timer the same amaount of imes you click on "start timer" it wont stop.

Igor-vkfl
Автор

Can you show how to use parameter in setInterval()?

alifsuria
Автор

Do you know how to do scripts for different functions for an online game I’ll give you an example javascript:setInterval(function()%7BStreetWarsRaidDialog.attack(0)%7D, 20), setInterval(function()%7BStreetWarsRaidDialog.attack(1)%7D, 20), setInterval(function()%7BStreetWarsRaidDialog.attack(2)%7D, 20), setInterval(function()%7BStreetWarsRaidDialog.attack(3)%7D, 20), setInterval(function()%7BStreetWarsRaidDialog.attack(4)%7D, 20); but there’s other things you can do on the game also that I don’t know how to write a JavaScript for them I just need help on it if at all possible

acethomas
Автор

please record with a larger font size so that the codes are more readable on screen. Thanks

MusicEffekt
Автор

Sir please provide 120 seconds time kill JavaScript

chvishnu