简体   繁体   中英

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. I have aa 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.

Thanks!

Use Date and document.cookies to update your variable. Also i used two utility methods to manipulate 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 ). 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.

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;
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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