如何每天在Java / javascript中更改变量的值一次 [英] How to change the value of a variable once per day in Java/javascript

查看:92
本文介绍了如何每天在Java / javascript中更改变量的值一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的学校制作一份申请/脚本,以显示当天的时间表。问题在于,我的学校每8天运行一次,因此事情变得很复杂。我有一个名为 cycleDay 的变量,但是如何每天只更新一次,最多更新一次呢?如果您有其他方法可以考虑这样做,请告诉我。

I'm trying to make an application/script for my school which displays the day's schedule. The catch with this is that my school runs on an 8 day cycle, so that complicates things. I have a a variable called cycleDay , but how would I go about updating this just once per day, and not more than that? If there's another way you can think of doing this, please let me know.

谢谢!

推荐答案

您可以使用< Date 对象的code> getTime()函数,该函数返回1970年1月1日之后的当前时间(以毫秒为单位)(来源:< a href = http://www.w3schools.com/jsref/jsref_gettime.asp rel = nofollow> http://www.w3schools.com/jsref/jsref_gettime.asp ),然后保存值(例如,在 var lastUpdateTime 中)。然后定期检查当前时间与所节省时间之间的差异是否超过一天。如果是这样,请更新 cycleDay ,并将 lastUpdateTime 更新到更新时间。

You can use, say, the getTime() function of the Date object, which returns the current time in milliseconds after January 1, 1970 (source: http://www.w3schools.com/jsref/jsref_gettime.asp), and save that value (e.g. in var lastUpdateTime). Then periodically check if the difference between the current time and that saved time is more than a day. If so, update cycleDay, and also update lastUpdateTime to the time when you updated.

例如,初始化为:

var lastUpdateTime = new Date().getTime();

代码中的其他地方:

var currentTime = new Date().getTime();
if(currentTime - lastUpdateTime >= 24*60*60*1000) // number of milliseconds in a day
{
    // update cycleDay
    lastUpdateTime = currentTime;
    // ...
}

这篇关于如何每天在Java / javascript中更改变量的值一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆