如何在Jquery中完成for循环后触发回调函数?

我有两个函数可以调整和调整屏幕上元素的高度和宽度。

panelHeight将目标元素的高度设置为可用的屏幕高度,而panelWidth则调整元素的宽度。

我的问题:
第一个函数( panelHeight )完成后,我无法确保第二个函数( panelWidth )触发。 如果目标元素很长并且有一个滚动条,它将被panelHeight删除但如果在panelWidth触发之前没有这样做,则设置宽度将被滚动条占用的空间关闭(17px – 当宽度为时仍然存在)计算)。

所以我正在寻找一种方法,只有在完成另一个function后才能触发一个函数。 有点像回调,但我不确定谁在下面for循环中摆弄这个:

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) }, 

所以我要么循环遍历推翻if-or-else循环而只需要触发panelWidth * AFTER *循环结束。

题:
知道如何摆脱超时并将panelWidth函数调用添加到循环结束? 我尝试了队列 ,但这也需要延迟(xxx) 。 另外我不能在for循环中触发panelWidth。 我只需要在高度function完成后触发一次

编辑
这可能是这样的:

 // calling my panelHeight function like this: self.panelHeight("source").done(function() { // call panelWidth: self.panelWidth(); }); 

如果是这样,我将在哪里放置var deferred = Deferred();

解决方案
得到它的工作。 谢谢大家! 这是我的解决方案:

调用panelHeight时 ,添加done()处理程序

 $(document).on('pagechange.fixedHeight', function() { self.panelHeight("pagechange").done( function() { self.panelWidth( false ) }); }); 

panelHeight中 ,声明延迟并返回resolved();

 panelHeight: function (from) { var deferred = $.Deferred(); ... run function return deferred.resolve(); } 

这是JQuery Deferred的目的……

 var deferred = $.Deferred(); somethingAsync(function() { // callback stuff here // now tell the deferred task it's ok to proceed deferred.resolve(); } deferred.done(function() { // your finalize code here }); 

编辑由于resolve事件需要与domresize相关联,因此Javascript resize事件处理程序可能会起作用。