我有兩個功能,可以在屏幕上調整和調整元素的高度和寬度。如何在jQuery中完成for循環後觸發回調函數?
panelHeight設定目標元素可用屏幕高度的高度,而panelWidth調整元件的寬度。
我的問題:
我不能保證第二功能(panelWidth)火災的第一功能後(panelHeight)完成。如果目標元素很長並且有一個滾動條,它將被panelHeight刪除,但如果在panelWidth之前沒有完成此操作,設置的寬度將會被滾動條佔用的空間(17px - 仍然存在當計算寬度時)。
所以我正在尋找一種方法來完成另一個功能後才能觸發一個函數。有點像回調,但我不知道是誰在撥弄這下面的循環:
panelHeight: function (from) {
var self = this,
o = self.options,
wrap = $('div:jqmData(wrapper="true").ui-page-active').last(),
overthrow = wrap.jqmData("scrollmode") == "overthrow" && $('html').hasClass('ui-splitview-mode'),
blacklist = $('html').hasClass('blacklist'),
// calculationg toolbars
// elements
contents = TARGET_ELEMENT;
if (overthrow) {
for (var i = 0; i < contents.length; i++){
// calculate values
...
contents.eq(i).css({
"max-height": setH,
"margin-top": blacklist == true ? glbH + lclH : 0,
"margin-bottom": blacklist == true ? glbF + lclF : 0
})
}
} else {
for (var i = 0; i < contents.length; i++){
// calculate values
...
contents.eq(i).css({
"max-height" : "",
"height": o._iPadFixHeight,
"margin-top": blacklist == true ?
parseFloat(lclH.outerHeight()) : 0,
"margin-bottom": blacklist == true ?
parseFloat(lclF.outerHeight()) : 0
})
}
}
// USING THIS NOW, WHICH IS NOT NICE
window.setTimeout(function(){ self.panelWidth(false); },65)
},
所以我無論是通過推翻 - 如果有或其他迴路循環只需要火panelWidth * AFTER *循環結束。
問:
任何想法如何擺脫超時和panelWidth函數調用添加到循環的結束?我嘗試隊列,但這也需要延遲(xxx)。此外,我無法在for循環中觸發panelWidth。我需要一次高度功能齊全它只有一次觸發
編輯:
這將是可能的:
// calling my panelHeight function like this:
self.panelHeight("source").done(function() {
// call panelWidth:
self.panelWidth();
});
如果是這樣,我在那裏將不得不把VAR推遲=遞延();?
解決方案:
得到它的工作。謝謝大家!這裏是我的解決方案:
當調用panelHeight,加()完成處理
$(document).on('pagechange.fixedHeight', function() {
self.panelHeight("pagechange").done(function() {
self.panelWidth(false)
});
});
內panelHeight,聲明延遲和返回解析();
panelHeight: function (from) {
var deferred = $.Deferred();
... run function
return deferred.resolve();
}
您的代碼中沒有異步函數。因此,所有代碼都是阻塞的,不需要使用回調函數,只需在代碼末尾調用該函數即可。如果這不起作用,問題不在於通話時間,而是其他問題。 – Yoshi