简体   繁体   中英

Delay function until jQuery .load is complete

I have a site using ajax to load the main content, and I am using the following code to run a function ( afterLoad ) after the .load() is complete..

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

Everything works fine but on occasion afterLoad runs before .load() is complete. Is there anyway to delay afterLoad to ensure everything is finished loading before it fires.

call the php function from jquery at the end of the script

something like

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

or

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

and in your function do a check to see if the argument is present, if so proceed else skip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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