admin管理员组

文章数量:1291050

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
}

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
}
Share Improve this question edited Mar 9, 2020 at 14:31 halfer 20.5k19 gold badges109 silver badges202 bronze badges asked Mar 29, 2012 at 15:55 NathanNathan 2191 gold badge10 silver badges22 bronze badges 3
  • I think this will be easier on the server side. On client side there are many catchs. Can you use a cron or something like that? – pollirrata Commented Mar 29, 2012 at 15:59
  • you seem to have a setTimeout() -- that only gets called once, but I assume you already know that. – arnorhs Commented Mar 29, 2012 at 16:00
  • 1 Have you thought about things like different time-zones etc.? – mbx-mbx Commented Mar 29, 2012 at 16:00
Add a ment  | 

3 Answers 3

Reset to default 7
var initDate = new Date(); // Or get the user login date from an HTML element (i.e. hidden input)
var interval;

function keepAlive() {
    // Do stuff (ajax call)
}

window.onload = function() {
    keepAlive();

    interval = window.setInterval(function() {
        var now = new Date();
        if(now.getTime() - initDate.getTime() < 8*60*60*1000 && now.getDate() == initDate.getDate()) {
            keepAlive();
        }
        else {
            // Stop the interval
            window.clearInterval(interval);
        }
    }, 5*60*1000);
}
function keepAlive(currDay) {        
    $.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() {
    var startDay = new Date().getDate();
    var startTime = new Date().getTime();
    var interval;
    keepAlive(startDay); //Make sure the function fires as soon as the page is loaded

    interval = setInterval( function () {
        if (startDay != new Date().getDate() || startTime < (new Date().getTime() - 1000*60*60*8)) {
            clearInterval(interval);
            return;
        }
        keepAlive();
    }, 300000); //Then set it to run again after five minutes
}

This might not be exactly what you asked for but I believe my answer will be useful to many other users. In my sample, setInterval repeats the action (of sending data to the server using AJAX) every 5 minutes. Below it is setTimeout function which clears the interval after 8 hours, using the interval ID. Hope it helps someone.

repeatUpdateSession();
function repeatUpdateSession() {
    var updateSession = window.setInterval(function() {
        var myemail = $.trim($("#myemail").html());
        $.post('includes/updatesession.php', {myemail:myemail}, function(data){
            //alert(data);
        });
    }, 5*60*1000);
    setTimeout( function() {
        clearInterval(updateSession);
    }, 8*60*60*1000);
}

本文标签: JavaScriptjQuery call function every 5 minutes for an 8 hour periodStack Overflow