javascript once per day

Solutions on MaxInterview for javascript once per day by the best coders in the world

showing results for - "javascript once per day"
Marta
28 Jan 2019
1// Run Javascript code to check for the day trigger
2
3// checks if one day has passed. 
4function hasOneDayPassed()
5  // get today's date. eg: "7/37/2007"
6  var date = new Date().toLocaleDateString();
7
8  // if there's a date in localstorage and it's equal to the above: 
9  // inferring a day has yet to pass since both dates are equal.
10  if( localStorage.yourapp_date == date ) 
11      return false;
12
13  // this portion of logic occurs when a day has passed
14  localStorage.yourapp_date = date;
15  return true;
16}
17
18
19// some function which should run once a day
20function runOncePerDay(){
21  if( !hasOneDayPassed() ) return false;
22
23  // your code below
24  alert('Good morning!');
25}
26
27
28runOncePerDay(); // run the code
29runOncePerDay(); // does not run the code
30