2012-01-11 19 views
1

メモリリークがあると思われるコードをデバッグしています。 Google Chromeでコードを実行すると、しばらくするとブラウザがクラッシュします。私は強く、次のコード(単純なために全体のコードではなく、テンプレートを添付)が問題を引き起こしていると信じています。 Chromeデベロッパーツールの[タイムライン]タブを使用して、実際のアプリケーションでメモリ使用量とメモリの急増を監視しました。トランジションを呼び出しているコードに気づいたら、トランジションとトランスティンシオンに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(); 

は、ここで私はプレーするあなたの呼び出しはtransitionTo関数内に間違っていると信じているコード

// 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

+0

このコードでは、メモリリークが発生することはありません。 – davin

+0

config.changeSpeedとは何ですか?あなたのconfig.speed以上の場合、それは確かに問題になる可能性があります。さらに、キャッシュされていない多くのDOMプロパティの取得/更新を毎秒実行しています。たとえば、要素名の一部をキャッシュして1秒ごとに検索する必要はありませんか? – Niklas

答えて

0

の長いバージョンです。あなたは議論を提供していません。ところで、なぜあなたはplay()も外部に呼び出しますか?可能であれば、コード全体を投稿してください。私はなぜあなたがトランジションにカウンター文字列を渡しているのかわかりません。 1つの提案は、このようなplayメソッドでsetIntervalメソッドを使用することです。

+0

'setTimeout'の代わりに' setInterval'を使うのは、ほとんどの場合常に悪い考えです。 – Niklas