如何延迟JavaScript JSON流一分钟
How to delay a JavaScript JSON stream for one minute?
我被要求编写一个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);
相关文章:
- Javascript JSON.parse not working
- Javascript:JSON请求仅在第一次工作
- 定义this.properties或objectName.properties的javascript JSON对象
- Javascript/JSON警报返回Undefined
- 使用XPath样式访问Javascript JSON对象属性
- JavaScript JSON关联对象键和值都作为日期时间戳
- Javascript-JSON-嵌套和分组
- Javascript json返回未定义
- 无标题Javascript JSON数组->CSV导出
- Javascript JSON对象替换对象中的字符串
- Javascript/Json日期转换问题
- 使用jstl c:forEach在迭代jsp bean上创建javascript json对象
- 为什么我的JavaScript / JSON脚本不能在Internet Explorer中工作
- 将数据从 Python 发送到 Javascript (JSON)
- JavaScript Json 值追加基于相等条件
- 在设置用户后,从 JavaScript JSON 请求中获取 Web 服务 (asmx) ASP.NET Windows
- JavaScript - JSON 数据结构的构建 - 如何使用变量值更改键名
- Javascript JSON 解析和字符串化用于多级
- 如何将数据 php 更改为 javascript json
- Javascript JSON 到 Excel 文件下载