开发者

Retaining setTimeOut() call even after page reload

开发者 https://www.devze.com 2023-02-08 01:42 出处:网络
I\'ve my site pages structure as 1) index.php which calls addline.php using ajax and the html returned is appended to the index.php

I've my site pages structure as 1) index.php which calls addline.php using ajax and the html returned is appended to the index.php 2) the addline.php calls another page more.php using ajax which again appends the returned html to it 3) Again more.php calls another file update.php and in the update.php, I've my following js codes

var number = parseInt("<?php echo $delFlag; ?>");
    if ( number == 1) {
        // Calling updateLine() function after 5 mins
        timer = setTimeout("updateLine()",1000*5*60);   

    }

    function updateLine() {
            var flagId = <?php  echo $flagId; ?>;
        var dataPass = 'flagId=' + flagId;
        $.ajax({
            type: "POST",
            url: "proc/updateLine.php",
           开发者_StackOverflow中文版 data: dataPass,
            cache: false,
            success: function(){
                // Show error if error is there
            }
        }); 
    }

All the time, my location is still index.php.

The javascript function works properly if I do not reload the page. If I reload the page, it doesn't work. I want the setTimeOut() call to be active in the background even after the reload takes place. It should trigger the function call after 5 mins.

How do I achieve it??


Reloading a page resets the Javascript state and there is no direct way to keep things running in the background.

If the requirement is to continue the timeout counter automatically after the page reload, then the counter state has to be persisted somehow.

It means that every timeout start has to be accounted for. One option would be to do it with PHP and load and unload events, along the lines of:

// timeout.php -- persists and returns the last timeout start by session
<?php

session_start();
$key = 'lastTimeoutStart';

if (isset($_GET[$key]))
    $_SESSION[$key] = $_GET[$key];
else if (isset($_SESSION[$key]))
    echo $_SESSION[$key];

?>

Plus the Javascript part that handles persisting and loading:

var lastTimeoutStart = 0;

if ( number == 1) {
    // Calling updateLine() function after 5 mins
    lastTimeoutStart = new Date().getTime();
    timer = setTimeout("updateLine()",1000*5*60);   

}

//
// Other code
//

$(document).load(function () {
    $.get('timeout.php', function (data, textStatus, jqXHR) {
        var persistedStart = data.lastTimeoutStart;
        var tempTimeout = persistedStart + 1000*5*60 - new Date().getTime();
        if (tempTimeout > 0) {
            clearTimeout(timer);
            timer = setTimeout("updateLine()", tempTimeout);
        }
    });
});

$(document).unload(function () {
    var data = {"lastTimeoutStart": lastTimeoutStart};
    $.get('timeout.php', data, function (data, textStatus, jqXHR) {});
});

There may be bugs in the above code but hopefully you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消