I want call function once in java script and for implement this idea I write this code and I use closure
function initialize() {
let called = 0;
return function() {
if (called > 0) {
return
} else {
called++;
console.log('view has been set!')
}
}
}
const start1 = initialize();
start1();
start1();
start1();
I call start1 function third time and when I run this code I get once this output "view has been set!" in console. But I notice I can call initialize function many times and create different start function for example
const start1 = initialize();
const start2 = initialize();
start1();
start2();
This time in output I have twice "view has been set!". How can I fix that. thanks.