0
以下代碼是我長期輪詢的前端代碼。在此我期待代碼setTimeout(getChat,0)異步調用方法。但是,當getChat方法的XHR處於掛起狀態時,不同方法的所有後續XHR也將進入掛起狀態。異步調用不工作在香草javascript
discussTask = function(taskId) {
taskIdChat = taskId
getChat() // initial call
}
var getChat = function() {
taskId = taskIdChat
payLoad = {
'task_id': taskIdChat,
'recent_message_id': recentMessageId
}
var xmlhttp = XHR('/chat/sync', 'POST', payLoad)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === 4) {
buildChat(JSON.parse(xmlhttp.responseText))
setTimeout(getChat, 0) // Async recursive call
}
}
}
var XHR = function(action, method, payLoad) {
var xmlhttp = new XMLHttpRequest()
xmlhttp.open(method, action, true)
xmlhttp.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xmlhttp.send(JSON.stringify(payLoad))
return xmlhttp
}
什麼是「不同的方法」?你爲什麼用相同的參數提出請求? – guest271314
請在陳述結尾加上';'。取決於ASI是危險的。 – Barmar
關於設置TaskIdChat和之後設置TaskId的問題,與之前設置TaskIdChat時相同的值是什麼? – Unlockedluca