首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript setTimeout内存问题

JavaScript setTimeout内存问题
EN

Stack Overflow用户
提问于 2012-01-11 19:17:31
回答 1查看 775关注 0票数 1

我正在调试一个代码,我认为它有一些内存泄漏。在Google Chrome中运行代码时,浏览器会在一段时间后崩溃。我强烈认为以下代码(为了简单起见,只附加了一个模板,而不是整个代码)导致了这个问题。当我使用Chrome developer tools的Timeline选项卡时,我观察到实际应用程序中的内存使用量和内存峰值很多。如果您注意到代码,play将调用transitionTo,并且transistionTo有一个setTimeout函数再次调用play。我认为内存并没有因为这个而被释放。有人能帮我解决这个问题吗?

代码语言:javascript
复制
    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();

下面是代码的较长版本

代码语言:javascript
复制
// 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');
                    }
                };
            };
        };
EN

回答 1

Stack Overflow用户

发布于 2012-01-11 19:24:11

我相信您在transitionTo函数中调用play是错误的。您没有提供参数。顺便问一下,为什么还要在外部调用play()?如果可能,请发布完整的代码。我不知道您为什么要将计数器字符串传递给transitionTo。一个建议是在play方法中使用setInterval方法,如下所示

代码语言:javascript
复制
function play(){
    //existing logic
    setInterval(function(){transitionTo("gallery","counter");},1000); 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8818311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档