I have this simple javascript function:
What you need is .one() function. It makes sure the code is triggered only once for you.
.one()
Docs: http://api.jquery.com/one/
Docs example:
$( "#foo" ).one( "click", function() { alert( "This will be displayed only once." ); });