简体   繁体   English

如何将功能延迟到加载动态页面的功能完成之后?

[英]How can I delay a function until after a function which loads a dynamic page is finished?

So I have a setup where I have a layout page, then I have separate content pages that are loaded into the layout on click. 因此,我有一个设置,其中有一个布局页面,然后有单独的内容页面,这些内容页面均在单击时加载到布局中。 The problem I'm having is initiating js only associated with those pages. 我遇到的问题是启动仅与这些页面相关联的js。 It go and get the page and while it's loading it will initiate the other functions, I see it when I step through it.. then it disappears the minute the page actually loads. 它去获取页面,并且在页面加载时将启动其他功能,当我逐步浏览页面时会看到它..然后它在页面实际加载的那一刻消失了。 So I believe the problem is as the questions says, I need to return the loaded page and then make the js call. 所以我相信问题出在问题上,我需要返回加载的页面,然后进行js调用。

This is one of my pages that is dynamically loaded on click, the morris(); 这是我的页面之一,它是在点击时动态加载的morris(); is another function to load my google map. 是加载我的Google地图的另一个功能。

$("#production").click(function () {
       $("#content").load("content/Mobile_Production.html");
       morris();
   });

This is the morris function.. 这是莫里斯功能。

   function map() {
    var locations = [['<h4>Office</h4>', 39.9629369, -105.1710262], ['<h4>Office</h4>', 39.388755, -107.082249], ['<h4>Office</h4>', 42.300884, -71.801840]];
    var iconURLPrefix = 'IMAGE HERE';
    var icons = [iconURLPrefix + 'cec-icon1.png', iconURLPrefix + 'cec-icon1.png', iconURLPrefix + 'cec-icon1.png']
    var icons_length = icons.length;
    var shadow = { anchor: new google.maps.Point(15, 33), url: iconURLPrefix + 'msmarker.shadow.png' };
    var map = new google.maps.Map(document.getElementById('map'), { zoom: 3, center: new google.maps.LatLng(38.318426, -97.985179), mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, streetViewControl: false, panControl: false, zoomControlOptions: { position: google.maps.ControlPosition.LEFT_BOTTOM } });
    var infowindow = new google.maps.InfoWindow({ maxWidth: 160 });
    var marker;
    var markers = new Array();
    var iconCounter = 0;
    for (var i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2], locations[i][3]), map: map, icon: icons[iconCounter], shadow: shadow });
        markers.push(marker);
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
            }
        })(marker, i));
        iconCounter++;
        if (iconCounter >= icons_length) {
            iconCounter = 0;
        }
    }

    function AutoCenter() {
        var bounds = new google.maps.LatLngBounds();
        $.each(markers, function(index, marker) {
            bounds.extend(marker.position);
        });
        map.fitBounds(bounds);
    }

    AutoCenter();
}

Simply add your morris() function in the second parameters of the load function like this: 只需在load函数的第二个参数中添加morris()函数,如下所示:

$("#production").click(function () {
       $("#content").load("content/Mobile_Production.html", morris);
});

JQuery load() documentation jQuery load()文档

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

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