问题描述
我正在使用以下代码加载广告网络:
<Border BorderThickness="1" BorderBrush="White"> <TextBlock ... /> </Border>
在$.getJSON('https://server.ads.io/api/v1/decision/?publisher=xx-com&ad_types=text-v1&format=jsonp',function (data) {
$('#ea-wrapper').html(data.html);
});
加载时间过长的情况下,浏览器将等待广告 ad aeternum 。我正在寻找的是一种等待3秒钟而没有响应的取消getJSON的方法。知道如何实现吗?
解决方法
您可以按照我的评论中的说明手动abort请求。
您需要将getJSON
设置为variable
,并在三秒钟后调用variable
中的setTimeout
来abort
请求。
setTimeout(function(){
//kill the request
adRequest.abort()
},3000) //3 seconds
您可以尝试的最终代码如下:
var adRequest = $.getJSON('https://server.ads.io/api/v1/decision/?publisher=xx-com&ad_types=text-v1&format=jsonp',function(data) {
$('#ea-wrapper').html(data.html);
});
setTimeout(function() {
//kill the request
addRequest.abort()
},3000) //3 seconds
如果您想使用$.ajax
,可以使用本机timeout
方法
$.ajax({
url: 'https://server.ads.io/api/v1/decision/?publisher=xx-com&ad_types=text-v1&format=jsonp',type: 'GET',dataType: 'jsonp',success: function(data) {
$('#ea-wrapper').html(data.html);
},timeout: 3000 //3 seconds
});