JavaScript Timers - setTimeout, setInterval, clearTimeout, and clearInterval

  Рет қаралды 33,255

Steve Griffith - Prof3ssorSt3v3

Steve Griffith - Prof3ssorSt3v3

7 жыл бұрын

How to use the built-in timer functions which allow you delay your function calls.
Code GIST: gist.github.com/prof3ssorSt3v...

Пікірлер: 66
@rotrose7531
@rotrose7531 4 жыл бұрын
Can not thank you enough for these god-sent tutorials. My only hope is that please make more and more. Your knowledge can help many people like me, and hope more students will find this channel, It is a waste of resource when I see the number of viewers.
@malikbrett2040
@malikbrett2040 2 жыл бұрын
I dont mean to be so off topic but does any of you know of a way to get back into an instagram account? I was dumb forgot my password. I appreciate any help you can offer me.
@WalrusMcDonald12n2na2
@WalrusMcDonald12n2na2 2 жыл бұрын
You explained this perfectly thank you so much. Was confused about assigning the timeouts to a variable name.
@chesterxp508
@chesterxp508 2 жыл бұрын
Brilliant tutorials :)
@MiSt3300
@MiSt3300 3 жыл бұрын
A bit long, but nevertheless effective. Thank you for your help, I couldn't get my head around these w3 schools and stack overflow tutorials (;
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
You have to be very careful with anything that you get from either of those resources.
@jrs_devs
@jrs_devs 3 жыл бұрын
I love the sound of the mouse click, together with the calm voice it's very relaxing
@KieranX
@KieranX 5 жыл бұрын
Thnx this was helpfull
@andrzejsotnikow7494
@andrzejsotnikow7494 3 жыл бұрын
Thank you!
@kelvinsantosalm
@kelvinsantosalm 4 жыл бұрын
Thank you.
@arctan2
@arctan2 4 жыл бұрын
thank you so much
@juliethabila-rn9uv
@juliethabila-rn9uv 4 ай бұрын
The best
@mostafagh3573
@mostafagh3573 2 жыл бұрын
great, thank u
@FordExplorer-rm6ew
@FordExplorer-rm6ew 4 жыл бұрын
Would there be a way to use setTimeout &| setInterval to send continuous http requests to something? Kind of like a scuffed slow http?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
Sure. You can put anything you want inside the functions that you pass to setTimeout or setInterval.
@justaprogrammer3764
@justaprogrammer3764 5 жыл бұрын
Aside from the interval and timeout methods, those i understand, something else hit me. How is declaring a variable initiating that function or its value? function test(){ console.log('this function was executed'); } let x = test(); let y = 10; In the variable y its being internally saved on memory and not computed with until asked for, but declaring a function automatically runs it ?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 5 жыл бұрын
Declaring the variable with the function doesn't run it. The parentheses after the function name are what make it run. let f = function(){ //this is a function stored inside the variable f return 'abc'; } let x = f; //now a reference to the function that is stored inside the variable f is also store in the variable x. let y = f( ); //now the function f will run and its result, 'abc' will be stored in the variable y.
@ayaanirshadbhat6567
@ayaanirshadbhat6567 3 жыл бұрын
​@@SteveGriffith-Prof3ssorSt3v3 I get what you said, but here's my question; -------------------------------------------------------------------------------- function log(msg) { console.log('MESSAGE', msg); clearInterval(tommy); } let tommy = setInterval(log, 500, 'Goodbye'); --------------------------------------------------------------------------------- In the code above, the last line is clearly a case of let y = f(); So if the result of f() [here setInterval] is going to be stored in y [here tommy], the function log has to run first and then the value is stored ( as per my understanding ), but here we see that the function log calls clearInterval(tommy) when the function is being run for the first time, how can tommy have a value at this point and clearInterval(tommy) not throw an error since the function hasn't completely run even once. Thanks
@bodaciouschad
@bodaciouschad 2 жыл бұрын
Ah. That is why you get an "invalid callback error" from setInterval(callback(deets),500); You have to use setInterval(callback,500,deets); Thank you.
@andrewgarfinkel1978
@andrewgarfinkel1978 4 ай бұрын
What if you wanted to setInterval function to run immediately, as well as at each interval? So for the first call...you don't want any delay.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 ай бұрын
There is no way to avoid a delay because the function being given to the timer is always placed in a holding area off the main stack. Even setting a time of zero, there will be a delay. Here is a video about this process - kzbin.info/www/bejne/rV7Nhn-OZalomck I would suggest that you use a named function inside the timer like this: setInterval( myFunc, 1000); and then you can call the function before setting the timer: myFunc( ); setInterval( myFunc, 1000);
@andrewgarfinkel1978
@andrewgarfinkel1978 4 ай бұрын
@@SteveGriffith-Prof3ssorSt3v3 thanks. I watched a video that described JavaScript as sort of like a “one lane highway”…where there is a “main line”, and a “finish line.” Basically, all the synchronous code with go in order on the “main line.” But when something is async, it is pulled off the “main line,” and temporarily, it is pulled into “another lane on the highway,” until it is ready to go back onto the “main line” of the highway. Is this an accurate way to think of it? So basically, you are suggesting to make the first call synchronous, and the rest of them will be async?
@IvoTsochev
@IvoTsochev 3 жыл бұрын
I didn't get why I would want to clear the setTimeout since it's running only once?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
You might want to clear a setTimeout BEFORE it completes for the first time.
@de-kat
@de-kat 2 жыл бұрын
Are the Timers asynchron? Like can i run othere code while the are counting ?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 2 жыл бұрын
Yes they are.
@de-kat
@de-kat 2 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 thx
@Martin958
@Martin958 Жыл бұрын
What if you want setInterval( ) to run more than just once? How would you do that? And what is the point of setInterval( ) if you can't turn it off normally?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
SetInterval automatically runs over and over and over until you stop it with clearInterval.
@Martin958
@Martin958 Жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Yes but clearInterval( ) will stop it before it even begins as you showed. And also hiding it in another function only runs it once. It's hard to see how you would stop it after (x) number of runs.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 Жыл бұрын
@Martin958 you would need a counter variable outside the scope of the timer. Increment the counter inside the timer function. If the value exceeds your max then call clearInterval
@Martin958
@Martin958 Жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Perfect, thank you.
@chvishnu43
@chvishnu43 5 жыл бұрын
Clear time
@ayroserosli4313
@ayroserosli4313 4 жыл бұрын
How can I set the delay time if it's offline when the data in database is delayed after 3 minutes?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
The timers don't care about online or offline, they just want to call a function after a delay. They will call the function regardless of any other settings or things that are going on unless the webpage is closed.
@ayroserosli4313
@ayroserosli4313 4 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Thank you for the information. Basically like this, my project is about air quality monitoring which is involve the sensor. So i need to display the status of sensor which is online when the sensor is running then if it's stop the status will display offline. So, I want to implement the delay time if the data is stop within 3 minutes the status will offline. How can I do it? :(
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 4 жыл бұрын
There should be some event that is triggered by the sensor to tell you when it goes online or offline. Create a listener for that event which will run a function that starts your timer.
@ayroserosli4313
@ayroserosli4313 4 жыл бұрын
Steve Griffith oh okayokay thank you so much 😊
@kllokoq
@kllokoq 3 жыл бұрын
I've been trying to figure this out, just recently started to study JS. Why does this happen if I write this: var tick = true; function ticker() { if(tick) { console.log("Tick"); tick = false; } else { console.log("Tock"); tick = true; } } setInterval (ticker, 1000); Everything works as expected. It tick tocks every second. But if I place the variable tick within the scope of the function ticker, it's value is not changed to false, and the console.log keeps printing only tick. It confuses me I feel that there is something very important for me to understand here!
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
You need to understand scope and hoisting kzbin.info/www/bejne/fqbOloOtopKLfZo kzbin.info/www/bejne/fKq6laV5j7-UrLM I have other videos on those topics too.
@kllokoq
@kllokoq 3 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 Thank you! This makes much more sense now!
@kllokoq
@kllokoq 3 жыл бұрын
​@@SteveGriffith-Prof3ssorSt3v3 What I didn't like about the code I wrote in the comment is that it had this global variable. I wanted to have code with everything it needed to run, locally. And I just learned that is possible by nesting a function within a function, and creating what is called a closure! This is the new code: function x() { var tick = true; function ticker() { if(tick) { console.log("Tick"); tick = false; } else { console.log("tock"); tick = true; } } return ticker; } var test = x(); setInterval(test, 1000);
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
I have a couple videos on closures too. kzbin.info/www/bejne/pXTQfaxtmsiEiLc kzbin.info/www/bejne/gWrJm4COa6tonKc You would probably learn a lot going through my JS from the Start playlist - kzbin.info/www/bejne/sJu5d6icqZaCaLs
@kllokoq
@kllokoq 3 жыл бұрын
@@SteveGriffith-Prof3ssorSt3v3 I already have! I love them! I'm trying to cover the same subjects from different sources, as a way to learn through repetition and gain different perspectives!
@chvishnu43
@chvishnu43 5 жыл бұрын
Prepare a script please
@piyushgoswami8909
@piyushgoswami8909 3 жыл бұрын
which ide is this ?
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 3 жыл бұрын
Brackets from Adobe, which is what I used before switching to VSCode
@muhammedcansoy1434
@muhammedcansoy1434 3 жыл бұрын
That ıs what I am lookıng
@chvishnu43
@chvishnu43 5 жыл бұрын
And also clear 120 second timer
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 5 жыл бұрын
I cant understand what you are saying. Please use full sentences and provide details about your questions
@chvishnu43
@chvishnu43 5 жыл бұрын
Please help me brother
@chvishnu43
@chvishnu43 5 жыл бұрын
With out login
@chvishnu43
@chvishnu43 5 жыл бұрын
Clear 20 second time script sir
@chvishnu43
@chvishnu43 5 жыл бұрын
Hi Sir PL provide a script for captcha above URL
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 5 жыл бұрын
Please post requests on the tutorial request video
@chvishnu43
@chvishnu43 5 жыл бұрын
There is 20 second time how to kill it
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 5 жыл бұрын
ClearTimeout or clearInterval are how you stop a timer.
@chvishnu43
@chvishnu43 5 жыл бұрын
Sir please create a script for automatic captcha and auto login
@chvishnu43
@chvishnu43 5 жыл бұрын
Open the url and next u can find out the 20 seconds countdown time
@FordExplorer-rm6ew
@FordExplorer-rm6ew 4 жыл бұрын
@@chvishnu43 lol you need to work on those social engineering skills guy
@sethortiz697
@sethortiz697 5 жыл бұрын
It's hard to focus with the keyboard clicks and the sound of the computer fan. If you used an external microphone, your videos would 1,000 more enjoyable. Otherwise awesome video.
@SteveGriffith-Prof3ssorSt3v3
@SteveGriffith-Prof3ssorSt3v3 5 жыл бұрын
yeah. Very aware. I got a good external mic over a year ago. Just my old videos that have the unfortunate audio issues.
JavaScript Callback Functions
11:56
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 33 М.
Manipulating and Sorting Arrays in JavaScript
12:32
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 21 М.
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,5 МЛН
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 28 МЛН
ОДИН ДЕНЬ ИЗ ДЕТСТВА❤️ #shorts
00:59
BATEK_OFFICIAL
Рет қаралды 8 МЛН
Understanding the Keyword THIS in JavaScript
13:59
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 8 М.
The Hidden World of requestAnimationFrame
9:22
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 31 М.
#29. Таймеры в JavaScript (setTimeout, setInterval, clearTimeout, clearInterval)
10:22
Using the setInterval() function in JavaScript
11:22
dcode
Рет қаралды 44 М.
JavaScript Try...Catch plus Throwing Errors and Exceptions
11:19
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 17 М.
The Async Await Episode I Promised
12:04
Fireship
Рет қаралды 1,1 МЛН
JavaScript Array filter method
9:15
Steve Griffith - Prof3ssorSt3v3
Рет қаралды 18 М.
Lambda Calculus - Computerphile
12:40
Computerphile
Рет қаралды 1 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,5 МЛН