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

fre*_*ent 2 javascript jquery for-loop callback

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

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

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

所以我正在寻找一种方法,只有在另一个功能完成后才能触发一个函数.有点像回调,但我不确定谁在下面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)
    },
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:
这是可能的:

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

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

解决方案:
让它发挥作用.谢谢大家!这是我的解决方案:

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

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

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

panelHeight: function (from) {  
    var deferred = $.Deferred();

    ... run function

    return deferred.resolve();
    }
Run Code Online (Sandbox Code Playgroud)

McG*_*gle 6

这是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
});
Run Code Online (Sandbox Code Playgroud)

编辑 由于resolve事件需要与dom调整大小相关联,因此Javascript 调整大小事件处理程序可能会起作用.