我希望进行长时间轮询以将某些数据“推送”到客户端,并且我还在与长轮询并行的情况下对服务器进行其他无关的AJAX调用.在长轮询收到响应(来自响应或超时)之前,我的其他AJAX调用似乎无法完成.当我单步执行Javascript时,似乎第二个AJAX请求是在适当的时间发送的,但是在长轮询请求得到响应之前没有收到响应.知道发生了什么事吗?
以下是长轮询部分的代码:
服务器端:
function getPlaylistTracksIfChanged($playlist_id,$numClientTracks) {
$reportChange = false;
for($i = 0; $i < 10; $i++) {
$numServerTracks = $this->PlaylistTrack->find('count',array(
'conditions' => array('playlist_id' => $playlist_id)
)
);
if($numClientTracks != $numServerTracks) {
$reportChange = true;
break;
}
sleep(3);
}
if($reportChange) {
$playlist_tracks = $this->PlaylistTrack->find('all',array(
'conditions' => array('playlist_id' => $playlist_id),'order' => array('PlaylistTrack.position')
)
);
$this->set('playlist_tracks',$playlist_tracks);
$this->layout = false;
$this->render('show_playlist_tracks_list');
} else {
$this->autoRender = false;
return 'false';
}
}
客户端:
function checkForChangesOnServer() {
$.post('/getResultsIfChanged/' + playlist_id + '/' + $('#sortable_tracks').children().size(),function(results) {
if(results == 'false') {
//alert('no change');
} else {
//alert('change');
}
checkForPlaylistChangesOnServer();
});
}
以及另一个AJAX调用的示例:
服务器端:
function getLibraryTracksStartingWithLetter($user_id,$letter) {
$results = $this->Track->find(
'all',array(
'conditions' => array(
'user_id' => $user_id,'OR' => array(
'Track.artist LIKE' => $letter . '%','Track.name LIKE' => $letter . '%'
)
),'order' => array('case when Track.artist = "" then 1 else 0 end','Track.artist','Track.name')
)
);
$this->set('results',$results);
$this->layout = false;
$this->render('show_library_results_list');
}
客户端:
function loadLibraryResultsForLetter(letter) {
highlightLetterFilter(letter);
$.post('/getLibraryTracksStartingWithLetter/' + user_id + '/' + letter,function(results) {
updateLibraryResults(results);
});
}
最佳答案
原文链接:https://www.f2er.com/jquery/428409.html