var x = 0; var intervalID = setInterval(function () { // Your logic here if (++x === 5) { window.clearInterval(intervalID); } }, 1000);