0

How good/bad is to call a function every second in a website ?

Hello everybody. I am taking the JavaScript course and I found the following code to print out the current date. function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+secs; } setInterval(printTime, 1000); As far as I know is good practice to add your scripts at the end of the body in html. This allows the website to load before the script runs. However I was wondering if calling a function will decrease the performance of the website once it is loaded. Here the function is simple, but what if the function do something else more complex?

29th Jul 2020, 4:11 PM
Oscar Santos
Oscar Santos - avatar
3 ответов
+ 1
Not to worry, for machine, 1 second interval is considered a very long long period. a CPU with a clock speed of 2 GHz can carry out two thousand million (2 000 000 000) cycles per second.
29th Jul 2020, 4:21 PM
Calviղ
Calviղ - avatar
0
So there won't be a problem if a function is called every mili second because it will run on the local client CPU ? What about complexity. Does that affects ? I mean let's say that every mili seconds calculate some complex maths.
30th Jul 2020, 2:15 PM
Oscar Santos
Oscar Santos - avatar