繁体   English   中英

JavaScript setTimeout内存问题

[英]JavaScript setTimeout Memory Issue

我正在调试一个我认为有一些内存泄漏的代码。 在Google Chrome浏览器中运行代码时,浏览器会在一段时间后崩溃。 我坚信以下代码(为简单起见,仅附加模板,而不附加整个代码)是造成此问题的原因。 当我使用Chrome开发者工具的“时间轴”标签并查看内存使用情况时,实际应用中的内存大量增加。 如果您注意到该代码,则播放正在调用transitionTo和transistionTo具有setTimeout函数,该函数再次调用play。 我认为因此无法释放内存。 谁能帮我解决这个问题?

    var htmlRotatorTimer = '';

    function play(src){
      // some code here
      transitionTo("gallery", "counter");
      // some code here
    }

    function transitionTo(gallery,index) {      
        // some code here      
       clearTimeout(htmlRotatorTimer);
       htmlRotatorTimer = setTimeout( function(){ play(); }, 1000 );    
       // some code here
     }

play();

这是代码的较长版本

// utility for loading slides
        function transitionTo(gallery,index) {

            // preloader (optional) 
            var counterMin = 0;
            if (config.preloader) {
                counterMin = 1;
                if (gallery.length < 3) pause('');                  
            }

            var oldCounter = counter;
            if((counter >= gallery.length) || (index >= gallery.length)) { counter = counterMin; var e2b = true; }
            else if((counter < counterMin) || (index < 0)) { counter = gallery.length-1; var b2e = true; }
            else { counter = index; }



   // added attr speed in div's
//htmlRotatorTimer = ''; 
         var itemSpeed = Number($(gallery[counter]).attr("speed") != undefined ?   $(gallery[counter]).attr("speed") : config.speed);
            //htmlRotatorTimer = setInterval(function(){ play(); },itemSpeed);
            clearTimeout(htmlRotatorTimer);
            htmlRotatorTimer = setTimeout( function(){ play(); }, itemSpeed );

            var rmkName = $(gallery[counter].children).attr("id") != undefined ? 'RMK_' + $(gallery[counter].children).attr("id") : '';
            var isHtml5 = false;
            if (rmkName != '' && eval ('typeof '+ rmkName) == 'object') {                   
                rmkObj = eval(rmkName);     
                isHtml5 = rmkObj.rmkType == 'html5' ? true : false;
                //console.log('html5 is' + rmkObj.rmkType,'obj name' + rmkName, 'typeof:' +(typeof rmkObj));            
            }

    if (config.effect=='fade') {    
        $(gallery[counter])
        .clone()
        .appendTo($cont)
        .hide()                     
        .fadeIn(config.changeSpeed,function(){$('#showbanners.rmkbanner').css({ 'visibility': 'visible'});if($.browser.msie)this.style.removeAttribute('filter');});                        
                if($cont.children().length>1){
                    $cont.children().eq(0).css('position','absolute').fadeOut(config.changeSpeed,function(){$(this).remove();});
                };
            } else if (config.effect=='none') {
                $(gallery[counter])
                    .appendTo($cont);
                if($cont.children().length>1){
                    $cont.children().eq(0).remove();
                };
            };
            // update active class on slide link
            if(config.links){
                $('.'+uniqueClass+'-active').removeClass(uniqueClass+'-active jshowoff-active');
                $('.'+uniqueClass+'-slidelinks a').eq(counter).addClass(uniqueClass+'-active jshowoff-active');
            };

            // reset for html5 objects only
            if (isHtml5) {
                    rmkObj.preload = 'nopreload';
                    rmkObj.Reset();
            }
        };// end function transistionTo

        // is the rotator currently in 'play' mode
        function isPlaying(){
            return $('.'+uniqueClass+'-play').hasClass('jshowoff-paused') ? false : true;
        };

        // start slide rotation on specified interval
        function play(src) {
            if (!finalStop) {
                if (!isBusy()) {
                    counter++;
                    transitionTo(gallery, counter);
                    if (src == 'hover' || !isPlaying()) {
                        //htmlRotatorTimer = setInterval(function(){ play(); },config.speed);
                        clearTimeout(htmlRotatorTimer);
                        htmlRotatorTimer = setTimeout(function(){
                            play();
                        }, config.speed);
                    }
                    if (!isPlaying()) {
                        $('.' + uniqueClass + '-play').text(config.controlText.pause).removeClass('jshowoff-paused ' + uniqueClass + '-paused');
                    }
                };
            };
        };

我认为您的播放电话在transitionTo函数中是错误的。 您没有提供任何论据。 顺便说一句,为什么还要在外面调用play()? 如果可能,请发布整个代码。 我不确定为什么要将计数器字符串传递给transitionTo。 一个建议是在这样的播放方法中使用setInterval方法

function play(){
    //existing logic
    setInterval(function(){transitionTo("gallery","counter");},1000); 
}

暂无
暂无

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

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