I have been tasked with creating a JavaScript program that retrieves an API JSON record from a specific address every minute through a WebSocket. The stream should continue after 60 seconds, and I need to return both the current stream and the previous one that was retrieved. Below is the code I have written:
var obj=
{
seconds : 60,
priv : 0,
prevTick : '' ,
data : ''
}
function countTime()
{
obj.seconds --;
obj.priv ++;
var message ;
if(obj.priv > 1)
{
obj.priv = 0;
obj.message = null;
}
if(prop.seconds < 0)
{
message = sock.open();
obj.message = obj.message + ", New Tick : " + message.message ;
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 ;
};
Can someone please help me identify what is incorrect in my code above? It doesn't seem to be delaying as expected, and the stream continues continuously.