2012-01-11 52 views
1

我正在調試一個代碼,我認爲它有一些內存泄漏。在Google Chrome中運行代碼時,瀏覽器會在一段時間後崩潰。我堅信下面的代碼(只附加一個模板,而不是簡單的整個代碼)就是這個問題。當我使用Chrome開發人員工具「時間軸」選項卡並觀看實際應用程序中的內存使用情況和內存高峯時。如果您注意到播放正在調用的代碼transitionTo和transistionTo有一個setTimeout函數再次調用播放。我認爲記憶不會因爲這個而被釋放。有誰能幫我解決這個問題嗎?JavaScript setTimeout內存問題

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'); 
        } 
       }; 
      }; 
     }; 
+0

那裏的其他代碼是什麼,你列爲「這裏的一些代碼」的東西? – Niklas 2012-01-11 11:22:23

+0

此代碼不會導致高內存使用率(更不用說內存泄漏)。 – davin 2012-01-11 11:24:16

+0

什麼是config.changeSpeed設置?如果它不是你的config.speed,那肯定會成爲一個問題。另外,你還有很多未緩存的DOM屬性檢索/更新,你每秒都在做什麼,你是否考慮過緩存一些元素名稱,這樣就不必每隔一秒都要搜索一次? – Niklas 2012-01-11 11:50:17

回答

0

我相信,你的電話打是不對的transitionTo函數中的更長的版本。你沒有提供論據。順便說一下,你爲什麼還要在外面打play()?如果可能,請發佈整個代碼。我不知道爲什麼你要將計數器字符串傳遞給transitionTo。一個建議是使用像這樣的播放方法的setInterval方法

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

使用'setInterval'而不是'setTimeout'在大多數情況下總是一個壞主意。 – Niklas 2012-01-11 11:26:30