开发者

Time generated events in html

开发者 https://www.devze.com 2023-01-27 01:42 出处:网络
I would like to generate an event say an alert box or any other events such as a text to appear in 开发者_如何学编程a window, at say, 10 am daily, on my webpage. How should i do? Checking time every f

I would like to generate an event say an alert box or any other events such as a text to appear in 开发者_如何学编程a window, at say, 10 am daily, on my webpage. How should i do? Checking time every frequently would consume resources, I assume.


Just calculate number of milliseconds between now and alarm time and pass the resulting number to setTimeout()


When you give the web page to the client, figure out how many milliseconds until 10 AM, then render this to your webpage:

<script type="text/javascript">
  setTimeout(function() { alert("Hey! It's 10 freakin AM!"); }, 10000);
</script>

...where 10000 is the number of milliseconds until 10 AM.

Of course, this would give you the number of milliseconds until 10 AM by the server's current time and in the server's current time zone, unless you knew the user's time zone and factored that into your calculation of the number of milliseconds until 10 AM.


If you are concerned about setting an event to check every minute or every 20 seconds whether it is 10:00am, then you can setTimeout using the upcoming 10am timestamp minus the current timestamp (make sure passing in milliseconds to setTimeout). Then when your processing is done, do the same thing to set an event for the next day.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号