📜  javascript 每天一次 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:09.082000             🧑  作者: Mango

代码示例1
// Run Javascript code to check for the day trigger

// checks if one day has passed. 
function hasOneDayPassed()
  // get today's date. eg: "7/37/2007"
  var date = new Date().toLocaleDateString();

  // if there's a date in localstorage and it's equal to the above: 
  // inferring a day has yet to pass since both dates are equal.
  if( localStorage.yourapp_date == date ) 
      return false;

  // this portion of logic occurs when a day has passed
  localStorage.yourapp_date = date;
  return true;
}


// some function which should run once a day
function runOncePerDay(){
  if( !hasOneDayPassed() ) return false;

  // your code below
  alert('Good morning!');
}


runOncePerDay(); // run the code
runOncePerDay(); // does not run the code