当我使用ajax加载新部分时,Javascript setTimeOut会以任何方式触发

发布于 2024-12-27 18:19:45 字数 1901 浏览 1 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2025-01-03 18:19:45

您的 reset() 函数会清除 setTimeout,然后启动新的 setTimeout。每次调用reset()时,您都会有一个新的setTimeout(),因此您应该为最后一个超时设置一个单独的重置函数。

reset = function () 
                {
                    clearTimeout(timer); // clears the Timeout
                    timer = setTimeout(logout, delay); // Starts a new timeout
                    //alert(timer);
                };

realReset = function ()
                {
                  clearTimeout(timer); //clears the timeout definitely
                }

我希望我是有用的。

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.

reset = function () 
                {
                    clearTimeout(timer); // clears the Timeout
                    timer = setTimeout(logout, delay); // Starts a new timeout
                    //alert(timer);
                };

realReset = function ()
                {
                  clearTimeout(timer); //clears the timeout definitely
                }

I hope I've been useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文