如何延迟JavaScript JSON流一分钟

How to delay a JavaScript JSON stream for one minute?

本文关键字:JavaScript JSON 一分钟 延迟 何延迟      更新时间:2023-09-26

我被要求编写一个java脚本程序,从和地址检索api JSON记录,并通过websocket每分钟。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);

EDIT:最后,当您完成时,只需执行

clearInterval(interval);