Created
          March 18, 2011 17:11 
        
      - 
      
- 
        Save iadvize/876448 to your computer and use it in GitHub Desktop. 
    a fonction to run some code no more than 1 time per period
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function runOnce(wait, cb){ | |
| var timeOut = null | |
| , launched = false; | |
| return function(){ | |
| if (launched === false){ | |
| launched = true; | |
| cb(arguments); | |
| } | |
| if(timeOut){ | |
| return; | |
| } | |
| timeOut = setTimeout(function(){ | |
| launched = false; | |
| timeOut = null; | |
| }, wait); | |
| }; | |
| } | |
| /* How to use: | |
| var tesFunc = runOnce(3000, function (args) { | |
| console.debug('testFunc executed'); | |
| }); | |
| tesFunc(1);tesFunc(1);tesFunc(1); | |
| setTimeout(function(){tesFunc(1);tesFunc(1);},5000); | |
| */ | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment