簡體   English   中英

JavaScript回調

[英]Javascript callbacks

我使用ajax獲取位置列表,然后根據從ajax結果創建的數組標記Google地圖。 我如何使回調函數具有Ajax調用,構造數組,標記所有同步的google map。(未調用updateMapMarkers())這是我的代碼。 謝謝

// main function to do work  
// need timer calling getLocations()      
function loadMapList() {  
    // initial google map here  
    var count = 40;  
    $("#countdown").html(count + " seconds remaining!"); 
    getLocations();   
    count--;  
    timeout = setInterval(function(){   
      $("#countdown").html(count + " seconds remaining!");   
      if (count == 0) {  
        count =40;  
        getLocations();  
      }  
    count--;  
    }, 1000);  
}  
// using ajax to get location info  
function getLocations(){  
    var url = "getLocations";  
    $.ajax({  
    type: 'GET',   
    url: url,  
    dataType: 'json',  
    async: false,   
    success: function(data){  
    if (data.locationList == null || data == 'undefined') {  
      return;    
    }   
    allLocArray.length = 0;   
    for (i in data.locationList) {    
      allLocArray[i] = new Array(3);    
      allLocArray[i][0] = data.locationList[i].LOCATE_NAME;  
      allLocArray[i][1] = data.locationList[i].LATITUDE;    
    allLocArray[i][2] = data.locationList[i].LONGITUDE; 
    }
    },   
    error: function(xhr, textStatus, error){
    alert(xhr.statusText);
    alert(textStatus);
    alert(error);
    }
    });
    }  
// mark google map using global var array
function updateMapMarkers() {  
    var myOptions = {  
    zoom: zoomLevel,  
    center: new google.maps.LatLng(centerLat, centerLong),  
    mapTypeId: google.maps.MapTypeId.ROADMAP  
    }  
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
    for (var i=0; i<allLocArray.length; i++) {  
      var myLatLng = new google.maps.LatLng(allLocArray[i][1], allLocArray[i][2]);  
      var marker = new google.maps.Marker({  
        position: myLatLng,  
        map: map,  
        title:allLocArray[i][0]  
      });  
    }   
} 

您可以簡單地將回調函數作為參數傳遞:

function getLocation(callback){
  ...
  callback();
  ...
}

function updateInfo(){
  ...
}      

getLocation(updateInfo);




編輯:您可以將函數鏈接在一起,以實現“串行”執行的感覺:


1.使用數組

function foo(callbacks){
    ...
    for(c in callbacks)
        c();
}

function bar(){
    ...
}
function baz(){
    ...
}

foo([bar, baz]);


2.使用回調鏈接

function foo(callback){
    ...
    callback()
}

function bar(callback){
    ...
    callback()
}
function baz(callback){
    ...
    callback()
}

foo(function(){
    bar(function(){
        baz(function(){
            alert('done');
        });
    });
});


3.使用類

class MyClass
{
    function foo(){
        ...
        return this;
    }

    function bar(callback){
        ...
        return this;
    }
    function baz(callback){
        ...
        return this;
    }
}

var obj = MyClass();
obj.foo().bar().baz()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM