Creating a function onceEvery(), that allows a function to be called only once every x milliseconds. The first result is able to return, but the function doesn't stop it from being recalled before cooldown time is completed, returning the previous result instead. Thanks in advance!
onceEvery = function (func, wait) {
//return function then can only return every * seconds
let cooldown = false;
let result;
let previous;
//if called again while cooldown = true, return previous result
if (!cooldown) {
result = func;
previous = result;
beginCoolDown();
return result;
} else {
return previous;
}
function beginCoolDown () {
cooldown = true;
setTimeout (function () {
cooldown = false;
}, wait);
}
}