0
我有一個隊列,可以將消息推送到我想要連續處理的隊列中。Javascript:監視隊列/連續運行功能
我之所以需要一個隊列,是因爲消息來得太快,無法完成處理!
這裏是我的代碼:
var messageQueue = [];
var ws = ...;
//When I get a socket.io message...
ws.on('message', function(data)
{
//Add it to the queue
addToQueue(data);
});
//Function that adds it to the queue:
function addToQueue(fullMessage)
{
messageQueue.push(fullMessage);
},
//Function that I'd like to run constantly
function fetcher()
{
while (messageQueue.length > 0)
{
//get the next message on the queue
var msg = messageQueue.shift();
handleMessage(msg);
}
//fetcher()?
}
//Function that works with the message
function handleMessage(fullMessage)
{
//do things with the message
}
我如何能得到「提取器」運行隨時有隊列中的項目的任何想法?
每次嘗試我做我結束了意外遞歸調用它,並打破了網頁:(
這會導致遞歸問題,我得到「最大的調用堆棧大小超出了」錯誤:/我已經試過「的setTimeout 「之前,延遲1秒,但它做了同樣的: – Kayvar 2015-02-11 19:33:59
你不應該得到一個遞歸問題'setTimeout()'是避免這種情況的方法。在你的代碼中必須有其他的東西導致它。 – 2015-02-11 19:35:04
因爲'setTimeout()'將傳遞給它的函數添加到一個叫做事件循環的隊列中,等待這個調用堆棧爲空 – 2015-02-11 19:36:48