Run javascript function only once

javascript, jquery

Solution

What you need is `.one()` function. It makes sure the code is triggered only once for you.

Docs: http://api.jquery.com/one/

Docs example:

$( "#foo" ).one( "click", function() {
  alert( "This will be displayed only once." );
});

Problem

I have this simple javascript function: ``` <script type="text/javascript"> var popup = '0'; if(popup == '0') { $(document).ready(function () { $(document).on('click', '.button', function(){ alert('test'); popup = '1'; }); }); } </script> <button class="button">Test</button> ``` I want the function to alert only on the first click but it keeps working although I changed the value of `popup` to 1

Original source