繁体   English   中英

jQuery中的for循环完成后,如何触发回调函数?

[英]How to fire a callback function after a for-loop is done in Jquery?

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

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

因此,我要么遍历“如果是否则否则遍历”循环,只需要触发panelWidth * AFTER *循环就完成了。

题:
任何想法如何摆脱超时,并将panelWidth函数调用添加到循环的末尾? 我尝试使用队列 ,但这也需要delay(xxx) 我也不能在for循环内触发panelWidth。 我只需要在高度功能完成后才触发一次

编辑
这可能吗:

// 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内部,声明延迟并返回resolve () ;。

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事件需要与dom调整大小相关联,因此Javascript 调整大小事件处理程序可能会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM