JavaScript 计时器 |随机行为
JavaScript timer | Random behavior
我有一个要求在一定时间间隔后显示一个弹出窗口,其中倒计时计时器运行 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 编码方面的专家,从上面的代码可以看出,有人可以看看吗?
非常感谢。
谢谢!
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);
我有一个要求在一定时间间隔后显示一个弹出窗口,其中倒计时计时器运行 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 编码方面的专家,从上面的代码可以看出,有人可以看看吗?
非常感谢。
谢谢!
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);