Dont use setTimeout function if you want to show quotes daily because when you will close your application then setTimeout will vanished as it will not run in background or when you will reload App then it will also reset. So its not good practice to use it for that purpose. If you want to show random quotes daily then make a backend and here you have to write CRON Job for it which will be executed after 24 hours and save quotes in your DB. And then load these quotes (by making backend route) from your DB on client side. However if you want to use the background timers then use them instead of setTimeout.
As Asad Haroon suggested, you may use crontab with a cronjob that looks something like this: 0 0 * * *
or @daily
. Apart from that, you may consider using anacron as well if you are not sure your backend runs non-stop 24/7 (but the config is different then).
But I want to suggest another implementation. You can try to record the last time (like formatted new Date()
) your user opened the website and saw the quote.
If you don't have a backend, you can store it using localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Once the user opens the website again, you check if 24 hours has passed since the last quote update. Say, subtract one date and time from the other. If it has, update the quote. Otherwise, don't do anything.
To summarize, make this calculation every time your user opens the website and check if 24 hours has passed since the last quote update by subtracting current date and time from the last quote update ones.