码迷,mamicode.com
首页 > Web开发 > 详细

js callback回调的一种写法

时间:2018-05-25 19:34:35      阅读:1154      评论:0      收藏:0      [点我收藏+]

标签:city   baidu   jsonp   local   var   cti   .ajax   address   htm   

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回调的一种写法

标签:city   baidu   jsonp   local   var   cti   .ajax   address   htm   

原文地址:https://www.cnblogs.com/muxueyuan/p/9089938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!