当我使用ajax加载新部分时,Javascript setTimeOut会以任何方式触发
当我使用 ajax 加载新部分时,Javascript setTimeOut 会以任何方式触发。我编写了一个 JavaScript 函数来检查页面是否空闲 5 秒,然后隐藏页面的标题和底部导航。在页面加载过程中,我单击新选项卡来加载新内容。当通过 jQuery 加载新部分时会发生什么,它会触发先前的 timeOut 函数。我被困住了。 这是我的等待和加载新部分的代码
等待代码检查空闲状态:
function checkIdleHome() {
//check the idle if no event occured
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
var events = ['ontouchstart','ontouchend','ontouchmove'],
i = events.length,
timer,
delay = 5000,
logout = function () {
// do whatever it is you want to do
// after a period of inactivity
//alert('lazy boy');
//alert('i am fired');
if(document.getElementById('mainnav')) {
document.getElementById('mainnav').style.display='none';
}
if(document.getElementById('header')) {
document.getElementById('header').style.display='none';
}
},
reset = function () {
clearTimeout(timer);
timer = setTimeout(logout, delay);
//alert(timer);
};
while (i) {
i -= 1;
document.addEventListener(events[i], reset, false);
}
reset();
//end idle if ios found
}//end of detecting navigator
}//end of check idle home
/// Code for load new section
var previd='';
function switchVideo(source,id,frame) {
document.getElementById(id).className='';
document.getElementById(frame).src=source;
if(previd!='' && id!=previd) {
document.getElementById(previd).className='inactiveVideo';
}
previd=id;
}
我想在使用此功能加载新选项卡时停止超时事件
Javascript setTimeOut fires any way when i load new section with ajax. I wrote up a javascript function to check if the page is idle for 5 seconds then hide header and bottom nav of my page. During the page load proccess i clicked the new tab to load new content. What happens when new section is loaded through jQuery it fires the previouse timeOut function. I am stuck with it.
Here is my code of wait and load new section
Wait code check idle state:
function checkIdleHome() {
//check the idle if no event occured
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
var events = ['ontouchstart','ontouchend','ontouchmove'],
i = events.length,
timer,
delay = 5000,
logout = function () {
// do whatever it is you want to do
// after a period of inactivity
//alert('lazy boy');
//alert('i am fired');
if(document.getElementById('mainnav')) {
document.getElementById('mainnav').style.display='none';
}
if(document.getElementById('header')) {
document.getElementById('header').style.display='none';
}
},
reset = function () {
clearTimeout(timer);
timer = setTimeout(logout, delay);
//alert(timer);
};
while (i) {
i -= 1;
document.addEventListener(events[i], reset, false);
}
reset();
//end idle if ios found
}//end of detecting navigator
}//end of check idle home
/// Code for load new section
var previd='';
function switchVideo(source,id,frame) {
document.getElementById(id).className='';
document.getElementById(frame).src=source;
if(previd!='' && id!=previd) {
document.getElementById(previd).className='inactiveVideo';
}
previd=id;
}
I want to stop the timeout event when i loaded new tab with this function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 reset() 函数会清除 setTimeout,然后启动新的 setTimeout。每次调用reset()时,您都会有一个新的setTimeout(),因此您应该为最后一个超时设置一个单独的重置函数。
我希望我是有用的。
Your reset() function clears the setTimeout and then starts a new setTimeout. Every time you call reset() you'll have a new setTimeout() so you should have a separated reset function for the last timeout.
I hope I've been useful.