function count(start, end) { let timeout_id let cancel = false function doPoint(number) { console.log(number) if(number === end) { cancel = true } if(cancel) { return ; } else { timeout_id = setTimeout(() => doPoint(number+1), 100) } } doPoint(start) return { cancel: function() { clearTimeout(timeout_id) } } }