浅谈JS异步轮询和单线程机制
2018-06-24 02:19:28来源:未知 阅读 ()
单线程特点执行异步操作
setTimeout(function(){
console.log(这是timeout事件回调);
},1000);
解决方案——异步
setTimeout(function(){
console.log(这是timeout事件回调);
},1000);

<script type="application/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="application/javascript">
function urlencode (str) {
str = (str + '').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
var city= '深圳',$citycode=urlencode(city);
var url='http://v.juhe.cn/weather/index?format=2&cityname='+$citycode+'&key=c82727e986a4f6cfc6ba1984f1f9183a';
$.ajax({url: url,
dataType: "jsonp",
type:"get",
data:{location:city},
success:function(data) {
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>"
);
}});
</script>
结果如下所示:先打印cc1,cc222, 然后执行25行success中代码
问题
- Callback没有按照我们预期的顺序执行
- Callback不易于我们进行模块化管理
jQuery.Deferred
jQuery1.5的变化,语法糖改变,遵循开发封闭原则
$.ajax({url: url,
dataType: "jsonp",
type:"get",
data:{location:city}})
.done(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('该接口调用达到上限')
}
})
.fail(function(){
console.log("获取数据失败")
})
.done(function(data){
console.log(data)
})
Deferred的另一种写法:
$.ajax({url: url,
dataType: "jsonp",
type:"get",
data:{location:city}})
.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(){
console.log("获取数据失败1")
})
.then(function(data){
console.log(data);
},function(){
console.log("获取数据失败2")
})
执行结果如下图所示:

$.Defferred 对象封装 返回一个defferred对象
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)
});
同步和异步的区别
jQuery.Deferred和Promise 区别
Promise不能主动干预改变promise的结果,Deffrred可以去改变干预执行的结果
最后,如果想看promise相关内容,请移步https://www.cnblogs.com/fuGuy/p/9215884.html
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- jQuery异步提交表单的两种方式 2020-03-12
- 浅谈JS的基础类型与引用类型 2020-02-29
- Javascript Ajax异步读取RSS文档具体实现 2020-02-25
- JavaScript 异步调用 2020-02-07
- jQuery实现异步获取json数据的2种方式 2019-12-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash

