简体   繁体   English

我认为我的 javascript 计时器导致浏览器死机

[英]I think my javascript timers cause the browser to freeze up

I'm making a menu with a lot of functionality.我正在制作一个具有很多功能的菜单。 I have to implement it towards other developers system, so I haven't been able to successfully make a jsfiddle for it.我必须针对其他开发人员系统实现它,所以我无法成功地为它制作一个 jsfiddle。 However, what happens is that if I leave it for a few minutes on the page, and go back to activate it, the browser will freeze for a second or more, perhaps even causing a "This script is causing the page to freeze etc.".但是,如果我将它留在页面上几分钟,然后 go 重新激活它,浏览器将冻结一秒钟或更长时间,甚至可能导致“此脚本导致页面冻结等。 ”。

I think it's related to my timers.认为这与我的计时器有关。 So I was hoping someone could take a look at the functions that run with timers, as my other functions are triggered by mouseevents, and see if they make sense.所以我希望有人可以看看使用计时器运行的函数,因为我的其他函数是由鼠标事件触发的,看看它们是否有意义。 I know there's a lot of code, but if you specifically look for the timers I think someone with more experience than me can get the jist of it.我知道有很多代码,但是如果您专门寻找计时器,我认为比我更有经验的人可以了解它的要点。 I'm gonna have to buckle down and see if I get a fiddle to work if not.如果没有,我将不得不系好安全带,看看我是否能找到小提琴。

The button that triggers the delay itself is the ul.downArrow inside the second timer in the code.触发延迟本身的按钮是代码中第二个计时器内的 ul.downArrow。

/* Check if submenu should be closed */
var timer;
$jq('#subNavigationContainer').hover(
    function(){
        clearTimeout(timer);
    },
    function(){
        var checkNavVariation = $jq('#subNavigationContainer').hasClass('small');
        timer=setTimeout(function () {
            if(checkNavVariation == false){
                $jq('#subNavigationContainer').animate({
                    height: ['50px', 'easeOutSine']
                }, 600, 'linear');
                var testLevel = $jq('#navigationContainer ul li.pathed').size();
                $jq('#subNavigationContainer ul,#subNavigationContainer #subMenuDescription').fadeOut('fast');  
                setTimeout(function () {
                    /* Indicate we want breadcrumb to open */
                    $jq('#subNavigationContainer').addClass('isClosed');        
                    if(testLevel < 1) {
                        $jq('#navigationContainer ul li').removeClass('selected');      
                    }
                    $jq('#navigationContainer ul li').children('.selArrow').remove();   
                }, 600);
            }
        }, 1500);
    }
);

setTimeout(function(){
    $jq('ul.downArrow').click(function() {
        loadMenu();
    }); 
    setTimeout(arguments.callee,1200);
},1200)

/* Automatic check for breadcrumb */
var counter = 0;
setTimeout(function(counter){       
    var subNavVisible = $jq('#subNavigationContainer').css('display');
    var subNavClosed = $jq('#subNavigationContainer').hasClass('isClosed');

    if(subNavClosed == true) {
        if(counter == 0) {
            $jq('#subNavWrapper ul').remove();
            $jq('#subNavWrapper div.subMenuDescription').remove();  
        } else {
            $jq('#subNavWrapper ul.downArrow').remove();
            $jq('#subNavWrapper div.subMenuDescription').remove();  
            $jq('#subNavWrapper ul.levelTwo').remove();     
            $jq('#subNavWrapper ul.levelThree').remove();   
            $jq('#subNavWrapper ul.levelFour').remove();            

        }
        $jq('#subMenuDescription').remove();
        $jq('#navigationContainer ul.levelOne').children('li.pathed').each(function() {
            /* Mark the top menu */
            $jq(this).siblings('li').removeClass('hover').removeClass('selected');
            $jq(this).siblings('li').children('.selArrow').remove();
            $jq(this).addClass('selected').addClass('hover');
            var liWidth = $jq(this).width();
            liWidth = (liWidth / 2) - 15;
            $jq(this).append('<div class="selArrow" style="margin-left: '+liWidth+'px"></div>');
            var highestHeight = 50;
            $jq(this).children('ul').clone().appendTo('#subNavWrapper').addClass('cloned');             
            $jq(this).children('ul').children('li.selected').children('ul').clone().appendTo('#subNavWrapper').addClass('cloned');
            $jq(this).children('ul').children('li.selected').children('ul').children('li.selected').children('ul').clone().appendTo('#subNavWrapper').addClass('cloned');
            var hasLevel = $jq('#subNavWrapper ul.levelThree li.pathed').size();

            $jq('#subNavWrapper ul').css('display','none');
            /* Check for grey submenu area visible or not */
            var subNavVisible = $jq('#subNavigationContainer').css('display');
            if(subNavVisible != "block") {
                $jq('#subNavigationContainer').css('height','0px');
                $jq('#subNavigationContainer').css('display','block');
                $jq('#subNavigationContainer').addClass('small');
                $jq('#subNavigationContainer').animate({
                    height: [highestHeight+'px', 'easeOutSine']
                  }, 300, 'linear');
            } else if(subNavVisible == "block") {
                $jq('#subNavigationContainer').animate({
                    height: [highestHeight+'px', 'easeOutSine']
                  }, 300, 'linear');
            }
            $jq('#subNavWrapper ul').css('height','2px').css('width','200px').css('display','block');
            $jq('#subNavWrapper ul li').css('display','none');
            $jq('#subNavWrapper ul li.pathed').addClass('breadcrumb');
            $jq('#subNavWrapper ul li.pathed').fadeIn('fast');
            $jq('#subNavWrapper ul li.pathed:last').parent('ul').after('<ul class="downArrow"><li><img src="http://dev.ghostwriter.no/demo/altibox/images/arrow-down.gif" alt="Ekspander" /><span>Utvid meny</span></li></ul>');
            $jq('#subNavWrapper ul').animate({
                height: ['21px', 'easeOutSine']
            }, 100, 'linear');
            counter++;
            $jq('#subNavigationContainer').removeClass('isClosed');
        });
    } else if(subNavClosed == false) {

    }
    setTimeout(arguments.callee,750) 
},750)

there are timers checking if the menu is open, if not it will open a breadcrumb like area有计时器检查菜单是否打开,如果没有,它将打开类似面包屑的区域

It's better to handle a moment when menu is closing and trigger showing your breadcrumb like area.最好处理菜单关闭并触发显示面包屑状区域的时刻 No timeouts required.无需超时。

there's a timer checking for a click on an arrow in the breadcrumb, that will open the menu again有一个计时器检查是否单击面包屑中的箭头,这将再次打开菜单

Just handle click on an arrow in the breadcrumb and open menu in handler.只需处理单击面包屑中的箭头并在处理程序中打开菜单。 No timeouts required here.这里不需要超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM