简体   繁体   English

如何每天在Java / javascript中更改变量的值一次

[英]How to change the value of a variable once per day in Java/javascript

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. 问题在于,我的学校每8天运行一次,因此事情变得很复杂。 I have aa variable called cycleDay , but how would I go about updating this just once per day, and not more than that? 我有一个名为cycleDay的变量,但是我将如何每天仅更新一次,最多更新一次呢? If there's another way you can think of doing this, please let me know. 如果您有其他方法可以考虑这样做,请告诉我。

Thanks! 谢谢!

Use Date and document.cookies to update your variable. 使用Datedocument.cookies更新您的变量。 Also i used two utility methods to manipulate document.cookies 我也使用了两种实用方法来操作document.cookies

var today = parseInt(new Date().getTime()/(1000*3600*24))

var cookies = getCookies();

if(cookies["last_updated"] && cookies["last_updated"]<today) 
{
    /* update variable */
    setCookie("last_updated", today, 1);

}

/* utility methods starts, adding utility methods to simplify setting and getting    cookies  */
function setCookie(name, value, daysToLive) {
   var cookie = name + "=" + encodeURIComponent(value);
   if (typeof daysToLive === "number")
      cookie += "; max-age=" + (daysToLive*60*60*24);
   document.cookie = cookie;
 }

function getCookies() {
    var cookies = {}; // The object we will return
    var all = document.cookie; // Get all cookies in one big string
    if (all === "") // If the property is the empty string
       return cookies; // return an empty object
    var list = all.split("; "); // Split into individual name=value pairs
    for(var i = 0; i < list.length; i++) { // For each cookie
       var cookie = list[i];
       var p = cookie.indexOf("="); // Find the first = sign
       var name = cookie.substring(0,p); // Get cookie name
       var value = cookie.substring(p+1); // Get cookie value
       value = decodeURIComponent(value); // Decode the value
       cookies[name] = value; // Store name and value in object
  }
  return cookies;
}
/* utility methods ends */
private Date lastUpdate = now()-1;
private myDailyChahgedValue;

Integer getMyDailyChangedValue() {
 if (lastUpdate <> now()) {
  myDailyChahgedValue ++;
}

return value;

}

Please note it is an a draft of code what shows main idea 请注意,这是代码草案,显示了主要思想

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 (eg in var lastUpdateTime ). 您可以使用Date对象的getTime()函数,该函数返回1970年1月1日之后的当前时间(以毫秒为单位)(来源: http : //www.w3schools.com/jsref/jsref_gettime.asp ),然后保存该值(例如,在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. 如果是这样,请更新cycleDay ,并将lastUpdateTime更新为更新时间。

For instance, initialize with: 例如,初始化为:

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

Somewhere else in your code: 您代码中的其他地方:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM