1. 程式人生 > >js callback回調的一種寫法

js callback回調的一種寫法

form itl ESS pan mui sco location 寫法 ces

getLocation.cityname(latitude, longitude, function (data1) {
SetCityCallBack(data1);
});

定義方法:

var getLocation = {
        cityname: function (latitude, longitude, callback) {
            $.ajax({
                url: ‘http://api.map.baidu.com/geocoder/v2/?ak=es2Cuviaa2zdKUjtxjq8t5wn&callback=renderReverse&location=‘ + latitude + ‘,‘ + longitude + ‘&output=json&pois=1‘,
                type: 
"get", dataType: "jsonp", jsonp: "callback", success: function (data) { console.log(data); var province = data.result.addressComponent.province; var cityname = (data.result.addressComponent.city);
var district = data.result.addressComponent.district; var street = data.result.addressComponent.street; var street_number = data.result.addressComponent.street_number; var formatted_address = data.result.formatted_address; localStorage.setItem(
"province", province); localStorage.setItem("cityname", cityname); localStorage.setItem("district", district); localStorage.setItem("street", street); localStorage.setItem("street_number", street_number); localStorage.setItem("formatted_address", formatted_address); data2 = { latitude: latitude, longitude: longitude, cityname: cityname, province: province }; if (typeof callback == "function") { callback(data2); } } }); }, } function SetCityCallBack(obj) { $(".app_titles span").eq(0).find("a").html(obj.cityname); $(".mui-table-view").append(‘<li class="mui-table-view-cell" id="city"><a href="#" id="acity" style="color: #999999">‘ + obj.cityname + ‘</a></li>‘); GetArea(obj.cityname, obj.province); GetShopListInfo(1, obj.latitude, obj.longitude, obj.cityname, "", "", obj.province); }

用屬性的方法寫函數

以及函數的回調

js callback回調的一種寫法