Can clearInterval() be called inside setInterval()?
Yes you can. You can even test it:
var i = 0;
var timer = setInterval(function() {
console.log(++i);
if (i === 5) clearInterval(timer);
console.log('post-interval'); //this will still run after clearing
}, 200);
In this example, this timer clears when i
reaches 5.