/** * Naturally, the preferred way is closure. * Here is a less popular but simpler & more * readable approach (since functions are * objects, use a semaphore-like property): * **/ function onceWhileExecuting() { if (onceWhileExecuting._locked_) { return onceWhileExecuting._locked_; } onceWhileExecuting._locked_ = true; setTimeout(function () { onceWhileExecuting._locked_ = false; console.log((new Date)); }, 1500); } onceWhileExecuting(); // (line 37) // this will return true - the function is "locked" (line 36) setTimeout(function () { console.log(onceWhileExecuting._locked_); onceWhileExecuting(); }, 500); // this will return false - the function is "unlocked" (lines 38, 39) setTimeout(function () { console.log(onceWhileExecuting._locked_); onceWhileExecuting(); }, 2500); // true // Mon Jun 15 2015 16:15:51 // false // Mon Jun 15 2015 16:15:53
„Времето е в нас и ние сме във времето, то нас обръща и ние него обръщаме.“ - Васил Левски