VB.net 2010 视频教程 VB.net 2010 视频教程 VB.net 2010 视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网站开发 > JavaScript >
  • JavaScript教程之浅谈JS异步轮询和单线程机制(4)

  • 2019-04-28 21:43 来源:未知

执行结果如下图所示:

 

 $.Defferred 对象封装 返回一个defferred对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function waitHandleWeather() {
    var dtd = $.Deferred()
    var wait =function(dtd){
        $.ajax({url: url,
            dataType: "jsonp",
            type:"get",
            data:{location:city}})
            .then(function(data){
                dtd.resolve(data)
            },function(){
                dtd.reject('该接口调用达到上限'//返回promise对象,如果返回dtd,外面就可以修改dtd的操作,不安全
            });
        return dtd.promise();
    }
    return wait(dtd); //返回promise对象
}
var weatherDataDeferred = waitHandleWeather();
$.when(weatherDataDeferred).then(function(data){
    if(data.result){
        var sk = data.result.sk;
        var today = data.result.today;
        var futur = data.result.future;
        var fut = "日期温度天气风向";
        $('#mufeng').html(
            "<p>" '当前: ' + sk.temp + '℃ , ' + sk.wind_direction + sk.wind_strength +
            ' , ' '空气湿度' + sk.humidity + ' , 更新时间' + sk.time +
            "</p><p style='padding-bottom: 10px;'>" + today.city + ' 今天是: ' + today.date_y +
            ' ' + today.week + ' , ' + today.temperature + ' , ' + today.weather + ' , ' + today.wind + "<p></p>"
        );
    }else {
        console.log('该接口调用达到上限')
    }
},function(err){
    console.log(err)
});
相关教程