Settimeout Only Once

by syamsoul

JavaScript

let setTimeoutOnce = function(func, timeout){
	if(typeof func != "function"){
  	console.log("ERROR:", "First parameter must be a function");
  }else if(isNaN(timeout * 1)){
  	console.log("ERROR:", "Second parameter must be number");
  }
	let st = null;
  
  this.exec = function(){
  	if(st == null){
      st = true;
      setTimeout(function(){
        func();
        st = null;
      }, timeout);
    }
  }
}

let the_timeout = new setTimeoutOnce(function(){
	alert('the settimeout only call once.. but you can call again after current settimeout finish');
}, 2000);

the_timeout.exec();
the_timeout.exec();
the_timeout.exec();
the_timeout.exec();
the_timeout.exec();

setTimeout(function(){
	the_timeout.exec();
},2300);