I searched for a script that did one function only once and found the function below
var something = (function() {
var executed = false;
return function () {
if (!executed) {
executed = true;
alert("olá");
}
};
})();
<a href="#" onclick="something()">Executar</a>
Well, it works perfectly!
But I did not understand why so much code since I summarized it that way and it works perfectly as well
var executada = false;
function chama() {
if (!executada) {
executada = true;
alert("olá");
}
};
<a href="#" onclick="chama()">Executar</a>
Any reason specified in the first code?