Hello, I am checking a JSON every x seconds to see if a specific thing has changed in it. This change will determine if a specific someone on twitch.tv has gone live. Once they do go live, it’ll output a console log saying they’ve went live and when they go offline.
Currently, it repeats the live or offline message. I need it to only output the status message once. How do I do this? This is my current code.
var twitch = 'https://api.twitch.tv/kraken/streams/mattypocket';
var online = false
setInterval(function() {
checkStream(twitch, function(data) {
data = JSON.parse(data)
if (data.stream && !online) {
online = true
console.log(`Mattypocket is online, playing ${data.stream.channel.game}. http://twitch.tv/mattypocket`)
} else {
online = false
console.log("offline");
}
});
}, 1000);
function checkStream(url, callback) {
request(url, function(err, response, body) {
if (!err && response.statusCode == 200) {
if (callback && typeof callback === "function") {
callback(body);
}
}
})
}