2011-01-27 69 views
0

我試圖檢測瀏覽器何時停止調整大小。以下工作,但取決於全球變量。沒有辦法使用閉包來避免這種情況嗎?如果是這樣,怎麼樣?ResizeStop沒有命名空間污染

var rtime = new Date(1, 1, 2000, 12,00,00); 
    var timeout = false; 
    var delta = 200; 

    $(document).ready(function() {   

     $(window).resize(function() { 
      rtime = new Date(); 
      if (timeout === false) { 
       timeout = true; 
       setTimeout(resizeend, delta); 
      } 
     }); 

    }); 

    function resizeend() { 
     if (new Date() - rtime < delta) { 
      setTimeout(resizeend, delta); 
     } else { 
      timeout = false; 
      $("#log").append("<span>resizeend</span> "); 
     }    
    } 

**腳本信用:http://vidasp.net/resizeend.html

回答

0
(function(){ 
var rtime = new Date(1, 1, 2000, 12,00,00); 
var timeout = false; 
var delta = 200; 

$(document).ready(function() {   

    $(window).resize(function() { 
     rtime = new Date(); 
     if (timeout === false) { 
      timeout = true; 
      setTimeout(resizeend, delta); 
     } 
    }); 

}); 

function resizeend() { 
    if (new Date() - rtime < delta) { 
     setTimeout(resizeend, delta); 
    } else { 
     timeout = false; 
     $("#log").append("<span>resizeend</span> "); 
    }    
} 
})(); 

是一個封閉

+1

不會當setTimeout的觸發它是不確定這項事業resizeend? – Matrym 2011-01-27 02:55:51