ajax 网络延迟 异步,利用 jQuery 的 Ajax 来实现检查网络延迟多少秒

原理

发起一个 Ajax 请求,开始记录一个时间,Ajax 返回 sucess 时的,记录这个时间为耗时时间,耗时时间-开始时间得出时间就是耗时的时间,通过耗时时间结果来根据 200,500 来设置辨别网络快慢

6624f76a76a60f86b48f11570f93ac70.png

代码

new Date ().getTime () 是获取毫秒

$.ajax ({

url: "/study/lag",

timeout: 3000,

success: function (data, text, xhr){

if (xhr.status == 200){

var lag = new Date ().getTime () - start;

if (lag < 200){

$("#lag").attr ("title", "网络畅通:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp.png")

}else if (lag < 500){

$("#lag").attr ("title", "网络较慢:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp_lag.png")

}else{

$("#lag").attr ("title", "网络很慢:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp_block.png")

}

}else{

$("#lag").attr ("title", "网络未连接").attr ("src", "/study/static/images/ic_network_cell_black_24dp_not.png")

}

},

error: function (){

$("#lag").attr ("title", "网络未连接").attr ("src", "/study/static/images/ic_network_cell_black_24dp_not.png")

},

complete: function (){

checkLag ();

}

})

上面只是一个请求,如果我们想隔 1 分钟检测一次呢,怎么办

写个函数,然后定时任务

// 延迟

function checkLag (){

setTimeout (function (){

var start = new Date ().getTime ()

$.ajax ({

url: "https://www.baidu.com",

timeout: 3000,

success: function (data, text, xhr){

if (xhr.status == 200){

var lag = new Date ().getTime () - start;

if (lag < 200){

$("#lag").attr ("title", "网络畅通:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp.png")

}else if (lag < 500){

$("#lag").attr ("title", "网络较慢:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp_lag.png")

}else{

$("#lag").attr ("title", "网络很慢:"+lag+"ms").attr ("src", "/study/static/images/ic_network_cell_black_24dp_block.png")

}

}else{

$("#lag").attr ("title", "网络未连接").attr ("src", "/study/static/images/ic_network_cell_black_24dp_not.png")

}

},

error: function (){

$("#lag").attr ("title", "网络未连接").attr ("src", "/study/static/images/ic_network_cell_black_24dp_not.png")

},

complete: function (){

checkLag ();

}

})

}, 1000 * 60)

}

checkLag ()

控制台

function checkLag (){

setTimeout (function (){

var start = new Date ().getTime ();

$.ajax ({

url: "https://www.heibaiketang.com",

timeout: 3000,

success: function (data, text, xhr){

if (xhr.status == 200){

var lag = new Date ().getTime () - start;

if (lag < 200){

console.log ("网络畅通:"+lag+"ms");

}else if (lag < 500){

console.log ("网络较慢:"+lag+"ms");

}else{

console.log ("网络很慢:"+lag+"ms");

}

}else{

console.log ("网络未连接");

}

},

error: function (){

console.log ("网络未连接");

},

complete: function (){

checkLag ();

}

})

},10*1000);

}

checkLag ();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值