Run part of a function only once until it's allowed to do otherwise - Javascript Function

Javascript examples for Function:Function Definition

Description

Run part of a function only once until it's allowed to do otherwise

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <button>Click Me</button> 
      <script>
var firstTime = true;//from   www  . j  a v a  2 s.  co  m
function myfunc(){
     if(firstTime){
        console.log("Triggering for the first time");
        firstTime = false;
     }
     console.log("Hi, How are you?");
}
document.querySelector("button").onclick = myfunc;

      </script>  
   </body>
</html>

Related Tutorials