2016-06-29 184 views
0

我有一些javascript函數。爲了停止重新調整大小事件兩次,我實施了一個修復程序,該修復程序迄今爲止一直運行良好。窗口調整大小不能觸發

function tabsResize() { 
    clearTimeout(timeout); 
    timeout = setTimeout(function() { 
     tabsUpdate(); 
    }, 200); 
} 

$(window).on("resize", tabsResize); 

但是,我剛剛創建了另一個函數,它在食物鏈中略微低一些,但是完全一樣。

var form = function() { 
    var pub = {}, timeout; 

function textareaAutoGrow() { 
    var pad = $(this).outerHeight(false) - $(this).innerHeight(); 

    this.style.height = "auto"; 
    this.style.height = (this.scrollHeight + pad) + "px"; 
} 

function textareaResize() { 
    alert("resize"); 
    clearTimeout(timeout); 
    timeout = setTimeout(function() { 
     textareaAutoGrow(); 
    }, 200); 
} 

function setupBindings() { 
    $("body").on("input", "textarea", textareaAutoGrow); 
    $(window).on("resize", textareaResize); 
} 

// PUBLIC FUNCTIONS 

pub.init = function() { 
    setupBindings(); 
} 

return pub; 
}(); 

問題是,它不是觸發 - 自動增長功能的作品,沒有javascript錯誤,我可以看到 - 它只是調整大小功能。

爲了檢查它是否實際上正在發射,我添加了警報。仍然沒有快樂。

但是,當我還向選項卡resize函數添加了第二個警報時,兩個警報都被觸發。我猜這是一種計時問題,但我不知道如何解決它。

有人建議嗎?

回答

0

first code demo

var _timeOut = null; 
 

 
function tabsResize() { 
 
    if(typeof(_timeOut)) 
 
     clearTimeout(_timeOut); 
 
    _timeOut = setTimeout(function() { 
 
     alert('=)'); 
 
    }, 200); 
 
} 
 

 
$(window).on("resize", tabsResize);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Second code Demo

var _timeOut = null; 
 

 
var form = function() { 
 
    var pub = {}, timeout; 
 

 
function textareaAutoGrow() { 
 
    alert('=)'); 
 
    var pad = $(this).outerHeight(false) - $(this).innerHeight(); 
 

 
    this.style.height = "auto"; 
 
    this.style.height = (this.scrollHeight + pad) + "px"; 
 
} 
 

 
function textareaResize() { 
 
    alert("resize"); 
 
    
 
    if(typeof(_timeOut)) 
 
     clearTimeout(_timeOut); 
 
    _timeOut = setTimeout(function() { 
 
     textareaAutoGrow(); 
 
    }, 300); 
 
} 
 

 
    $("body").on("input", "textarea", textareaAutoGrow); 
 
    $(window).on("resize", textareaResize); 
 

 
return pub; 
 
}();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

我還沒有安靜 - 只是測試。 –

+0

似乎與demo1中的修訂代碼保持一致 - 標記爲答案。 –