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
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.