I want my JavaScript to run once , but . I can not control how many times the javascript file is executed. Basically I write a tiny fragment of JS in CMS, and CMS actually calls it 5-10 times. So, such solutions:
function never_called_again(args) { // do some stuff never_called_again = function (new_args) { // do nothing } } never_called_again();
It doesn’t work, because as soon as my fragment starts again from above, the function is re-declared, and “do some things” is re-evaluated. Maybe I'm just not doing it right, I don't really like JS. I am considering using something like try-catch for a global variable, something like
if (code_happened == undefined) { \\ run code code_happened = true; }
EDIT: There is a consistent state, for example. if I set a variable, I can see when my fragment is running again. But, to declare it before I receive it, I don’t know how to say "does this variable exist"
Hamy
source share