setInterval working differently for a function with Global Variable & Local Variable! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

setInterval working differently for a function with Global Variable & Local Variable!

While trying to see whether Time changes if variable is outside the function and, Found that the function with local variable doesn’t change Time ! So can anyone explain me why does this happen? https://code.sololearn.com/WEm06Nrl88q0/?ref=app

19th Jun 2018, 2:29 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
12 Answers
+ 1
You get time from a var "d"... Now in "printtime" function you init (local) "d" any time that this function its called hence it will contain always up-to-date time... In "printer" function you dont init "d" (the global one that you have inited on line 15) but use it for get hour, minites and seconds and because your global "d" its setted only one time (on line 15) it will contain always same date hence in "printer" function you will retrieve always same hour, minutes and seconds.... Sorry i cant explain better
19th Jun 2018, 3:09 PM
KrOW
KrOW - avatar
+ 1
Your forget that on local one, you init all vars (hour, minute etc) to current time (because your get it at begin of function)... With global one you init d one time only before call it hence you will get same vars (eg. will be printed same time)
19th Jun 2018, 2:43 PM
KrOW
KrOW - avatar
+ 1
bro, function kulla variables irukum bodhu, setInterval thirumba thirumba call pannum bodhu, vary aayitey irukum.... but printer function la neenga just already set panna variables ah mattum dha call panreenga, so execute pannum bodhu irundha value mattum andha interval ku thirumba thirumba print aagudhu
19th Jun 2018, 2:50 PM
Hemath Kumar
Hemath Kumar - avatar
+ 1
Hemath Kumar Thanks nanba ippo purinjiruchu! 😁👌🏻
19th Jun 2018, 2:52 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
+ 1
namakula edhuku nanba thanks....!!😘
19th Jun 2018, 2:53 PM
Hemath Kumar
Hemath Kumar - avatar
+ 1
Discord open panniruken.... but epdi use panradhunu onnumey purila bro....😂
19th Jun 2018, 2:53 PM
Hemath Kumar
Hemath Kumar - avatar
+ 1
Hemath Kumar 😂😂 Bro Solo-Learn server la join pannitingala, Pannala na Intha code la Javascript la Link iruku join pannuga https://code.sololearn.com/Wu52hMg7JRmF/?ref=app
19th Jun 2018, 2:59 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
+ 1
KrOW No need not explain again coz i understand it now, Thanks man 😊🙌🏻
19th Jun 2018, 3:18 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
+ 1
👍👍👍
19th Jun 2018, 3:56 PM
KrOW
KrOW - avatar
0
KrOW I dont get it fully could explain it in a better way please?
19th Jun 2018, 2:50 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
0
Hemath Kumar Bro neenga discord la irukingala?
19th Jun 2018, 2:53 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
0
irunga poi paakura
19th Jun 2018, 2:59 PM
Hemath Kumar
Hemath Kumar - avatar