MSTC
MSTC

Reputation: 29

Execute Javascript once a week

I'm trying to change a text inside a canvas circle only once a week in wordpress The increment will always be the same.

How can I do it automatically? I suppose it has to do with server side javascript?

Thank you,

Upvotes: 0

Views: 671

Answers (3)

dakab
dakab

Reputation: 5875

Your use case doesn’t seem to be a critical security issue, so it might be okay to store the information date client-side. You can save a timestamp in localStorage and just do a integer comparison with something along the lines of:

var weekInMilliseconds = 7*24*60*60*1000; // == 604800000 ms
var lastInfo = parseInt(localStorage.getItem('info'), 10); // either NaN or timestamp

if(isNaN(lastInfo))
    lastInfo = 0; // 1970, mind you

// if last info showed earlier than one week ago:
if(lastInfo < (Date.now() - weekInMilliseconds)){
    localStorage.setItem('info',Date.now()); // set info date now
    alert('Information or action (once a week)'); // display your information
}

Upvotes: 1

PRDeving
PRDeving

Reputation: 669

You can set a localstorage item to have constance of the last update:

var date = new Date();
localStorage.setItem("last update", date.getDate());

And check the day on load:

window.onload = function(){
  var date = new Date();

  if(date.getDate() >= parseInt(localStorage.getItem("lastupdate"))+7){
    // RUN YOUR CODE
  }
}

Upvotes: 0

Luca Bruzzone
Luca Bruzzone

Reputation: 561

You can store the first time a page is loaded in your cookie or localstorage and then each time the page os loaded do a check with the current date.

With that said you should probably do this kind of things on the server side and retrive it on the client because if you're doing it in browser you have no control or replication on multiple devices

Upvotes: 0

Related Questions