0

Let's say it's June 18 and a person runs the javascript and it runs a function on load but it doesn't run the function later that day even if the person closes the window or program in my case. However, on June 19 if the person opens the program and it runs the javascript then it runs the function because it's a new day and hasn't run that day yet... So basically, how do I make an onload function only work once per a day? It has to be javascript because the program I'm using can only be assisted by javascript files and nothing else. The person has to download the files to their computer so how would I go about saving and retrieving the localStorage?

  • you have to store the flag in cookies or any persistent storage available to you – vitr Jun 17 '15 at 06:02
  • You'd need to store that you'd run it already for the day - perhaps a last run field? Depending on what browsers you need to support, `localStorage` or in a cookie? – Evan Knowles Jun 17 '15 at 06:03
  • possible duplicate of [How do I create and read a value from cookie?](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – rnevius Jun 17 '15 at 06:03
  • How do you identify a user? via a login? `LocalStorage` and `Cookies` are machine-dependent. So for example, if I use *laptop A* should I be prevented from running the javascript on *laptop B* ? – jasonscript Jun 17 '15 at 06:22

2 Answers2

1

You can use localStorage to achieve this. Sample Code:

/* returns null if local storage is not defined */
var localVal = localStorage.getItem('someUniqueName'); 

if(localVal  == null){ 
    // execute the function
}else{
    var tempd = new Date();
    var str = tempd.getDay() + tempd.getMonth() + tempd.getFullYear();
    if(localVal.localeCompare(str) == -1){
        //execute function
        localStorage.setItem('someUniqueName',str);
    }
}

If you wish to create a cookie and handle it, then you may go through this question

Community
  • 1
  • 1
Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32
  • 1
    Won't `localVal = null` always? Since it starts, does the function and stops. The next time it will keep doing the same thing and never run the `else` function which means `someuniqueName` never gets run. – Badrush Mar 07 '16 at 07:07
  • I also found a typo. You have two decimals here `+ tempd..getFullYear();` – Badrush Mar 07 '16 at 07:24
0

You can create a cookie on the user's browser that persists till the end of the day. If you are using jQuery, you can do as follows ( taken from here ):

var currentDate = new Date();
expirationDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1, 0, 0, 0);
$.cookie("ultOS", "5", {expires: expirationDate});

If you are using plain javascript, then the way to do this would be as follows (from here):

  function createCookie(name,value,date) {
        if (date) {
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

var today = Date();
var midnight = today.getDate()+1
midnight.setHours(0,0,0,0);
createCookie("Valid","true",midnight);

At the beginning of every page load, just check the value of this cookie.

Community
  • 1
  • 1
johngreen
  • 2,676
  • 5
  • 32
  • 47