I need to call a function every 5 minutes for an 8 hour period. The catch is it must be the on the same day. For example if the user logs onto the system at 11:59pm on 3/29 and it’s now 12:01am on 3/30 the function should no longer be called.
I know how to call it ever 5 minutes and have the jQuery ajax call coded; that part is fine. My problem is figuring out the date.
Here is the code:
var startDay;
function keepAlive(currDay) {
var today = new Date().getDate();
if (currDay == today) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{ alive: 'true' }",
url: "../ses/imsi_ses_edit.aspx/KeepSessionAlive",
dataType: "json",
success: function(data) {
},
error: function(response) {
alert(response.responseText);
}
});
}
}
window.onload = function() {
startDay = new Date().getDate();
keepAlive(startDay); //Make sure the function fires as soon as the page is loaded
setTimeout(keepAlive, 300000); //Then set it to run again after five minutes
}
1 Answer