Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript setInterval only output message once if changed

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #239824
    ErraticFox
    Participant

    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);
              }
            }
          })
        }
    
Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.