1

i have this JS Code:

<script>
$(document).ready(function () {
    window.setTimeout(function () {
        $('#logout_warning').reveal();
    }, 6000)
});
$(document).ready(function () {
    window.setTimeout(function () {
        location.href = "/login/logout.php?url=/index.php?r=inactivity";
    }, 12000)
});
</script>

it displays a DIV after X Seconds then redirects the page after Y Seconds

is there a way to create a link that will reset the timer back to X seconds and without having to refresh the page?

1
  • you need to bind a clearTimeout() to the click of your anchor element to reset your first setTimeout Commented Feb 27, 2014 at 1:06

1 Answer 1

1

You could write a wrapper for setTimeout like this

function myTimeout(fn, delay) {
    var o = {i: null};
    o.cancel = function () {
        if (o.i) window.clearTimeout(o.i);
    };
    o.reset = function () {
        if (o.i) window.clearTimeout(o.i);
        o.i = window.setTimeout(fn, delay);
    };
    o.reset();
    return o;
}

Then

// common (ancestor?) scope
var t;

// descendant scope, set
t = myTimeout(function () {
    $('#logout_warning').reveal();
}, 6000);

// descendant scope where you attach listener to your link
$('#my_link').on('click', function () {
    t.reset(); // re-start the timeout
    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.