延迟函数直到jQuery .load完成

时间:2013-10-08 01:50:52

标签: javascript jquery ajax

我有一个使用ajax加载主要内容的网站,我使用以下代码在afterLoad完成后运行一个函数(.load())..

jQuery('.main-menu a').click(function(evt){
        var href = jQuery(this).attr('href');
        var elementClassName = jQuery(this).attr('class');
        History.pushState({}, '', href);

        jQuery('.content').load(href+ ' #main',afterLoad);
            function afterLoad() { 
                jQuery('.content').fadeIn(750);
                if (document.location.pathname == "/works/") {
                    jQuery(function($){ window.dzsp_init("#port0",{
                        settings_slideshowTime:3
                        ,settings_mode: "masonry"
                        ,title: ""
                        ,design_item_width: ""
                        ,design_item_height: ""
                        ,design_categories_style: "normal"
                        ,design_categories_pos: "top"
                        ,design_pageContent_pos: "top"
                        ,settings_disableCats: "off"
                        ,settings_lightboxlibrary: "zoombox"
                        ,settings_preloadall: "off"
                        ,audioplayer_swflocation: "http://sbmdesigns.net/wp-content/plugins/dzs-portfolio/ap.swf"
                        ,videoplayer_swflocation: "http://sbmdesigns.net/wp-content/plugins/dzs-portfolio/preview.swf"
                        ,disable_itemmeta: "off"
                        ,settings_ajax_loadmoremethod: "button"
                        ,settings_mode_masonry_layout: "masonry"
                        ,design_total_height_full: "off"
                        ,settings_mode_masonry_layout_straightacross_setitemsoncenter: "off"
                        ,design_item_height_same_as_width : "off"})
                    });
                }
            }
         return false;
    });

一切正常但有时候afterLoad会在.load()完成之前运行。无论如何都要延迟afterLoad以确保在发生之前一切都已完成加载。

1 个答案:

答案 0 :(得分:0)

在脚本末尾的jquery中调用php函数

类似

$.ajax({ url: 'script.php?argument=value&foo=bar' });

$('#result').load('ajax/test.php');

并在您的函数中检查参数是否存在,如果是,请继续跳过