JSFiddle - React, Tailwind, and code Playground

by Vladimir Sobolev

JavaScript

window.requestInterval = function(fn, interval) {

	var time = Date.now,
		raf = window.requestAnimationFrame,
		start = time(),
		stop;

	function loop() {
		if (time() - start >= interval) {
			start = time();
			fn.call();
		}  
		stop || raf(loop);
	};

	raf(loop);
	return {
		clear: function() {
			stop = 1;
		}
	}
};

function test() {
	console.log('....');
}

var x = requestInterval(test, 2000);
setTimeout(function(){
	x.clear();
}, 5000);