I load a javascript library when a user clicks a button:
if (mylib.myfunction) {
mylib.myfunction();
} else {
var jsElt = document.createElement("script");
body.head.appendChild(jsElt);
jsElt.onload = function() { mylib.myfunction(); }
jsElt.src = MYLIB_URL;
}
That works, of course, but there is a problem: If the user clicks a second time before the onload event the else-part will be executed again. What is the best way to avoid this? A local variable? Something else?
UPDATE: I want to thanks @Suman Bogati and @usernid for their suggestion to disable the button that was clicked. However that solution is too limited for me because it assumes that you have access to the button from the code loading the new library. That is not always the case, of course.
In my case I can't use this solution so I will use a global variable instead. If no one has a better idea. ;-)