Javascript 功能活动超时不起作用
这是我的活动超时功能,但不起作用。我需要帮助才能知道原因,谢谢 首先,函数询问用户是否在这里:Javascript 功能活动超时不起作用,javascript,timeout,Javascript,Timeout,这是我的活动超时功能,但不起作用。我需要帮助才能知道原因,谢谢 首先,函数询问用户是否在这里: function activityTimeout(){ //$("#jquery_jplayer").jPlayer("pause"); clearTimeout(activityTO); blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO"&
function activityTimeout(){
//$("#jquery_jplayer").jPlayer("pause");
clearTimeout(activityTO);
blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton"> or <input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);
popupTO = setTimeout(abandonCorrection, 60000);
}
不能在同一函数中为同一超时清除超时和设置超时,因为函数在调用该函数时执行一次
function renewActivityTimeoutUnblock(){
$.unblockUI();
renewActivityTimeout();
}
function stopTimeout(){
clearTimeout(activityTO);
}
function renewActivityTimeout(){
clearTimeout(popupTO);
activityTO = setTimeout(activityTimeout, 1800000);
}
可能您认为它不起作用,因为您可能在javascript控制台中未定义此错误 确保声明了存储超时的变量
function activityTimeout(){
window.clearTimeout(activityTO);
// make sure that this is an existing function in you js code
blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton"> or <input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);
// make sure abandonCorrection is an existing function, and this will timeout for 1 minute
popupTO = window.setTimeout(abandonCorrection, 60000);
}
function renewActivityTimeoutUnblock(){
$.unblockUI();
renewActivityTimeout();
}
var activityTo; // declare this variable like this
var popupTo; // declare this variable like this
function renewActivityTimeout(){
window.clearTimeout(activityTO);
window.clearTimeout(popupTO);
// take note of your time here it's 1,800,000 ms that's about 30 minutes
activityTO = window.setTimeout(activityTimeout, 1800000);
}
记下你的时间,activityTimeout将在30分钟后生效。试着把它放低,这样你就能看见了。然后使用警报或控制台日志进行您自己的调试
这里是的一个简短演示,它被延迟了3秒钟,然后出现console.log和警报。希望这对你有帮助
注:
不要混淆setTimeout和setInterval
setTimeout只是一个延迟,只执行一次
另一方面,setInterval就像一个循环,它将在您设置的每个时间间隔内继续调用函数
要了解有关窗口计时器的更多信息放弃更正和blockInfoMsg是否是现有功能?请尝试使用简单功能,例如alert,检查您的功能是否工作。谢谢!但还是不行!事实上,在您的演示中,续订功能不起作用!这是放弃校正函数:函数放弃校正{window.onbeforeunload=null;window.location.reload;}@QUIEROHACERLO-ehhh?在演示中,您没有看到警报弹出窗口?3秒后显示。在JSFIDLE演示中,它只是代码的一个简短版本,只是为了向您展示SetTimeOutwork是如何工作的,我只看到一次!在我接受警报3秒钟后,什么也没发生@Quierohacero我想你把setTimeout和setInterval搞混了。请阅读我更新的答案。谢谢!是的,我很困惑。我希望在用户不执行任何操作时执行该函数,而不是每3秒或一次自动执行一次。再次感谢!
function activityTimeout(){
window.clearTimeout(activityTO);
// make sure that this is an existing function in you js code
blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton"> or <input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);
// make sure abandonCorrection is an existing function, and this will timeout for 1 minute
popupTO = window.setTimeout(abandonCorrection, 60000);
}
function renewActivityTimeoutUnblock(){
$.unblockUI();
renewActivityTimeout();
}
var activityTo; // declare this variable like this
var popupTo; // declare this variable like this
function renewActivityTimeout(){
window.clearTimeout(activityTO);
window.clearTimeout(popupTO);
// take note of your time here it's 1,800,000 ms that's about 30 minutes
activityTO = window.setTimeout(activityTimeout, 1800000);
}