簡體   English   中英

如何將JavaScript JSON流延遲一分鍾?

[英]How to delay a JavaScript JSON stream for one minute?

我被要求編寫一個Java腳本程序,每隔一分鍾從Web地址和地址檢索api JSON記錄。 60秒后,流繼續。 我期望返回各自的流檢索和先前檢索的流。 下面是我的代碼

var obj= 
{
    seconds : 60,
    priv : 0,
    prevTick : '' ,
    data : ''
}

function countTime()
{
    obj.seconds --;
    obj.priv ++;
    var msg ;
    if(obj.priv > 1)
    {
        obj.priv = 0;
        obj.msg = null;
    }
    if(prop.seconds < 0)
    {
        msg = sock.open();

        obj.msg = obj.msg + ", New Tick : " + msg.msg ; 
        setTimeout(countTime, 1000);
        obj.seconds = 60;
    }

}

var sock= new WebSocket('link');


sock.onopen = function(evt) {
    ws.send(JSON.stringify({ticks:'string'}));
};

sock.onmessage = function(msg) {
   var data = JSON.parse(msg.data);
   return 'record update: %o'+ data ;

};

請問我上面的代碼有什么問題嗎? 它根本不會延遲。 數據流繼續進行。

如何將緩沖行為封裝到一個類中?

function SocketBuffer(socket, delay, ontick) {
    var messages = [], tickInterval;

    socket.onmessage = function(msg) {
        messages.push( JSON.parse(msg.data) );
    };

    function tick() {
        if (typeof ontick !== "function") return;
        ontick( messages.splice(0) );
    }

    this.pause = function () {
        tickInterval = clearInterval(tickInterval);
    };
    this.run = function () {
        if (tickInterval) return;
        tickInterval = setInterval(tick, delay * 1000);
        tick();
    };
    this.run();
}

請注意, .splice(0)返回數組中的所有元素,並在同一步驟中清空數組。

用法:

var link = new WebSocket('link');
link.onopen = function (evt) {
    this.send( JSON.stringify({ticks:'string'}) );
};

var linkBuf = new SocketBuffer(link, 60, function (newMessages) {
    console.log(newMessages);
});

// if needed, you can:
linkBuf.pause();
linkBuf.run();

嘗試這個:

function countTime() {
    var interval = 1000; // How long do you have to wait for next round
    // setInterval will create infinite loop if it is not asked to terminate with clearInterval
    var looper = setInterval(function () {
        // Your code here

        // Terminate the loop if required
        clearInterval(looper);
    }, interval);
}

如果使用setTimeout() ,則無需手動計算秒數。 此外,如果您需要定期執行任務,則最好使用@RyanB所說的setInterval() setTimeout()對於只需要執行一次的任務很有用。 您還使用了prop.seconds但似乎未定義prop 最后,您需要在某個地方調用countTime() ,否則它將永遠不會執行。

這可能會更好:

var obj= 
{
    seconds : 60,
    priv : 0,
    prevTick : '' ,
    data : ''
}

function countTime()
{
    obj.seconds --;
    obj.priv ++; //I don't understand this, it will always be set to zero 3 lines below
    var msg ;
    if(obj.priv > 1)
    {
        obj.priv = 0;
        obj.msg = null;
    }
    msg = sock.open();
    obj.msg = obj.msg + ", New Tick : " + msg.msg; 
    obj.seconds = 60;
    //Maybe you should do sock.close() here
}

var sock= new WebSocket('link');

sock.onopen = function(evt) {
    ws.send(JSON.stringify({ticks:'string'}));
};

sock.onmessage = function(msg) {
   var data = JSON.parse(msg.data);
   return 'record update: %o'+ data ;

};
var interval = setInterval(countTime, 1000);

編輯 :最后,當您完成操作后,請執行

clearInterval(interval);

停止執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM