I have a few large forms that are causing problems with filling them out before the session times out. I need a little javascript to include at the end of the page to ping a file every few minutes to refresh the session timeout for as long as the page is open. So I have a function that the last statement is to call the function again after a gap. Also, for a range of reasons jQuery and other frameworks are not available for this. So I have put this together from https://stackoverflow.com/a/35970894/385011
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "json";
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
callback(null, xhr.response);
} else {
callback(status);
}
};
xhr.send();
};
//hearbeat
function heartbeat(heartbeaturl){
getJSON(heartbeaturl,function(err, data) {
console.log(Date.now());
console.log(data.time);
setTimeout(heartbeat(heartbeaturl), data.time);
});
}
console.log("run heartbeat");
heartbeat("heartbeat.json")
data.time is returning 60000 so it should run heartbeat every minute but it sin running every 1~4ms. What am I missing?