![set timer 1 minutes set timer 1 minutes](https://i.ytimg.com/vi/KB2QMfHOPrk/maxresdefault.jpg)
The familiar mechanical watch with hands appeared in China in 725 AD, designed by the masters Yixing (行) and Liang Lingzan (梁 令 瓚). Only a thousand years remained until the invention of the mechanical movement. The hourglass was invented around the 3rd century BC in the Middle East and Ancient Greece. Chinese and Indian water timers acted the other way around - an empty hemisphere with a small hole at the bottom, floating in the pool, gradually filled with water.
![set timer 1 minutes set timer 1 minutes](https://i.ytimg.com/vi/Pm2BvdiZUXA/maxresdefault.jpg)
In Babylon, Ancient Egypt, and Ancient Greece, water slowly flowed out of a filled vessel. The water clock helped people count the hours and minutes for a long time - the clepsydras heard the speeches of ancient Roman orators and helped the kings to be punctual. From the glass cylinder, water flowed out in a thin stream, perhaps then the expression "time is up" appeared. In the Middle Ages, time was measured by marks on candles.Ĭlepsydras or water clocks appeared about half a millennium later than the fire ones, they were invented by the ancient Babylonians and Egyptians. After the invention of glass, the functions of the fire clock were transferred to lamps with marked divisions - minutes and hours ran away along with the burnt out oil. When the stick decayed to the mark, the ringing of the ball that fell on the stand was heard. Metal balls were often attached to the marks. Sticks or spirals with marks were made from the mixture. The dry wood was ground into powder and mixed with incense. About 3000 years ago they were used in China, Japan, India, Greece and Persia. The ancient ancestors of timers are fire clocks. After the specified time has elapsed, an audible signal sounds.
![set timer 1 minutes set timer 1 minutes](https://static.vecteezy.com/system/resources/previews/000/555/361/original/countdown-timer-remaining-or-clock-counter-scoreboard-with-hour-minutes-and-seconds-display-vector.jpg)
Solution: add a callback function in useEffect hook to clear the interval in current scope so that only one setInterval() instance is running in the global environment at the same time.Timer is a device that measures a specified time interval from the moment of start. Clear setInterval() in this scope to avoid duplicated countdown.Solution: add a dependency of counter in useEffect hook so that every time when the counter changes, a new setInterval() is called. Let setInterval() get triggered every time when component gets re-rendered.
![set timer 1 minutes set timer 1 minutes](https://i.ytimg.com/vi/RA9KsHy6Ejo/maxresdefault.jpg)
To overcome the issue mentioned above, we need to trigger the setInterval() in every single App() call with different counter value, just as illustrated below. Third Attempts, useEffect with cancelling interval But in fact, it is not freezed, it is being reset all the time but the value is ALWAYS 59. That's why the counter seems to be freezed at 59. In the global environment, there is only one setInterval() instance which contiguously set the counter to 59, causing new App() calls always get the state counter to be 59. Therefore, within the App() scope, only in the first time, the useEffect() is triggered and the setInterval() is within the first time App() scope with the property counter always equal to 60. The following illustration may make things clearer.īecause every time when the component is re-rendered, the App() function is called again. This counter is indeed not decreased because the setCounter hook essentially does not change the counter within THIS function. But setCounter() definitely has run, then why isn't the counter updated? getElementById ( " root " ) render (, rootElement ) Įnter fullscreen mode Exit fullscreen modeĪll the numbers printed out are 60, which means the counter itself has not been decreased at all. Import * as React from " react " import const rootElement = document.