내가 checker
60 초마다과 같이라는 함수 실행을 중단하지 않고 호출여러 AJAX는
setInterval(checker, 60 * 1000);
checker
그것이 AJAX를 통해 확인 된 URL의 배열을 가지고가, 현재의 코드는 다음과 같다 :
$.ajax({
url: sites[i].url,
type: "GET",
dataType: "json",
async: false,
success: function(data){
//blah blah blah
}else{
//something else
},
error: function(){
//blah blah blah
}
});
코드가 작동하면 JSON 결과에 따라 일부 UI 기반 항목이 변경됩니다. 내 문제는 여러 사이트를 검사하는 실행 시간이 ~ 4 초이며,이 시점에 페이지가 응답하지 않게된다는 점입니다. async: false
을 제거하면 코드가 더 이상 예상대로 작동하지 않습니다.
누군가가이 문제를 해결하기 위해 콜백을 사용하여 언급했지만이 컨텍스트에서 사용하는 방법을 이해하지 못합니다.
편집
adosan에서의 제안에 따라업데이트 코드 : 콘솔에서
function promtest(){
var sites = [
{ name: "WorkingSite", url: "http://sitename.com/testing.php" },
//a bunch more sites, 1 returns a 404 to test for failure
{ name: "404Site", url: "http://404url.com/testing.php" }
];
var promiseList = [];
for(var i in sites){
var promise = $.ajax({
url: sites[i].url,
type: "GET",
dataType: "json",
async: true,
success: function(data){
if(data.Response != 'OK'){
console.log('Site ' + sites[i].name + ' Not OK');
}else{
console.log('Site ' + sites[i].name + ' OK ');
}
},
failure: function(data){
console.log('Failure for site: ' + sites[i].name);
},
error: function(){
console.log('Site ' + sites[i].name + ' Not OK');
}
});
promiseList.push(promise);
}
$.when.apply($, promiseList).then(function(){console.log('success')}, function(){console.log('fail')});
}
나는 참조 : 사이트 이름은 항상 마지막으로 보여주고있다
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
fail
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
Site 404Site Not OK
하는 것으로 하나는 목록에 있습니다.
당신이 비동기를 제거 할 때 작동하지 않는 것, 정교한하시기 바랍니다. –
비동기 적으로 실행하는 것을 고려 했습니까 (''async : true'')? – Crowes
위의 코드가 실제로 작동한다고 생각하지 않습니다. * else *는 함수 외부에 있습니다. – Robert