I use setInterval() into an anonymous function and I want to use clearInterval() in another anonymous function. I'm using socket.io and I need to have intervalID for each socket, so I can't use global variable.
I can clearInterval when clearLoops() is fired into the setInterval loop, but it doesn't work in my other socket events.
io.sockets.on 'connection', (socket) ->
intervalID = -1
clearLoops = ->
if intervalID != -1
clearInterval intervalID
intervalID = -1
socket.on 'first:action', (data, callback) ->
myArray = data
setTimeout(->
intervalID = setInterval(->
arrLength = myArray.length
if arrLength is 0
clearLoops()
while i < arrLength
if myArray[i].score >= MAX_SCORE
myArray.splice i, 1
i--
arrLength--
i++
, 300)
, 3000)
socket.on 'second:action', ->
clearLoops()
socket.on 'disconnect', ->
clearLoops()