Why is this time not running | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this time not running

var ele = document.getElementById("timer"); var timer; var sec = null; let myInterval = (function () { var sec = new Date(); let text = "Minnesota"; let result = text.fontcolor("green"); timer = setInterval(()=> { result = "<span style='color:green'>" + sec.toLocaleTimeString() + "</span>"; ele.innerHTML = result; if (sec == null){sec = time(myInterval, 1000)}; if (sec != null) {clearInterval(sec);} else {document.getElementById("tw").innerHTML = "time is greater than 5";} }, 1000) // each 1 sec } )(); https://code.sololearn.com/WLjHebAQH3nu/?ref=app

30th Sep 2022, 9:34 AM
Steve Nova
Steve Nova - avatar
5 Answers
+ 2
write specifically what error you are getting
30th Sep 2022, 10:48 AM
Davinder Kumar
Davinder Kumar - avatar
+ 1
so I understood he wants time to change all the time.
30th Sep 2022, 11:00 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
// Insert this to JS section function digitalClock() { var date = new Date(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); if (hours < 10) hours = "0" + hours; if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; document.getElementById("id_clock").innerHTML = hours + ":" + minutes + ":" + seconds; setTimeout("digitalClock()", 1000); } <!-- Insert this to HTML section and before and after call function digitalClock(); add tag script --> <div id="id_clock"></div> digitalClock();
1st Oct 2022, 6:41 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
In line 45 you are just re-using the initial time. If you want it to show the current time, you need to get a new date/ current time right before this line.
30th Sep 2022, 12:36 PM
Lisa
Lisa - avatar
0
No specific error I just wanted the code to change seconds automatically But I have rewritten the code
1st Oct 2022, 5:24 PM
Steve Nova
Steve Nova - avatar