JavaScript计时器|随机行为

时间:2015-02-10 20:37:37

标签: javascript timer settimeout setinterval oracle-adf

我需要在一定时间间隔后显示弹出窗口,其中倒数计时器运行5分钟,如果在倒计时变为0:00之前没有交互,则重定向到某个URL。我为此编写了以下js函数,但有时错误的计时器运行为负或停止执行!这是代码 -

function displayOnTimeOut() {
    window.docTitle = document.title;
    var outputText = AdfPage.PAGE.findComponent('root:popupTxt');
    outputText.setValue(' ');
    var timerInitDate = new Date();
    var endTime = timerInitDate.getMinutes() * 60 + timerInitDate.getSeconds() + 300;
    function displayCountdown() {
        var d = new Date();
        var timeNow = d.getMinutes() * 60 + d.getSeconds();
        var timeleft = endTime - timeNow;
        var seconds = timeleft % 60;
        var minutes = Math.floor(timeleft / 60);
        var minutesText = " minutes";
        if (minutes == 1) {
            minutesText = " minute";
        }
        var secondsText = " seconds";
        if (seconds == 1) {
            secondsText = " second";
        }
        if (seconds > 1 && seconds % 2 == 0) {
            document.title = document.title == docTitle ? 'News Flash' : docTitle;
        }
        if (timeleft == 0) {
            clearInterval(timerIntervalId);
            window.onbeforeunload = null;
            outputText.setValue(' ');
            window.location.href = '/some/url.jspx';
            return;
        } else {
            outputText.setValue(minutes + minutesText + " and " + seconds + secondsText);
        }
    }
    window.timerIntervalId = setInterval(displayCountdown, 1000);
    var popup = AdfPage.PAGE.findComponentByAbsoluteId('root:popup');
    popup.show();
}
var timeOutPeriod = 60 * 1000;
if (typeof timerTimeoutId !== 'undefined') {
    clearTimeout(timerTimeoutId);
}
if (typeof timerIntervalId !== 'undefined') {
    clearTimeout(timerIntervalId);
}
window.timerTimeoutId = setTimeout(displayOnTimeOut, timeOutPeriod);

请忽略调用方式/时间,我会在需要时使用ExtendedRenderKitService调用它。 我不是太多的JS编码专家,因为上面的代码很明显,请有人看看吗?

非常感谢。

谢谢!

1 个答案:

答案 0 :(得分:0)

function displayOnTimeOut() {
    window.docTitle = document.title;
    var outputText = AdfPage.PAGE.findComponent('root:popupTxt');
    outputText.setValue(' ');
    var timerInitDate = new Date();
    var endTime = new Date(timerInitDate.getTime() + 5*60000)
    function displayCountdown() {
        var timeNow = new Date();
        var timeleft = endTime - timeNow;

        if (timeleft <= 0) {
            clearInterval(timerIntervalId);
            window.onbeforeunload = null;
            outputText.setValue(' ');
            window.location.href = '/some/url.jspx';
            return;
        }

        var minutes = Math.floor(timeleft / 60000);
        var seconds = Math.floor(((timeleft % 60000) / 1000));

        var minutesText = " minutes";
        if (minutes == 1) {
            minutesText = " minute";
        }
        var secondsText = " seconds";
        if (seconds == 1) {
            secondsText = " second";
        }
        if (seconds > 1 && seconds % 2 == 0) {
            document.title = document.title == docTitle ? 'News Flash' : docTitle;
        }

        outputText.setValue(minutes + minutesText + " and " + seconds + secondsText);
    }
    window.timerIntervalId = setInterval(displayCountdown, 1000);
    var popup = AdfPage.PAGE.findComponentByAbsoluteId('root:popup');
    popup.show();
}
var timeOutPeriod = 60 * 1000;
if (typeof timerTimeoutId !== 'undefined') {
    clearTimeout(timerTimeoutId);
}
if (typeof timerIntervalId !== 'undefined') {
    clearTimeout(timerIntervalId);
}
window.timerTimeoutId = setTimeout(displayOnTimeOut, timeOutPeriod);