Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  14] [ 3]  / answers: 1 / hits: 30829  / 12 Years ago, mon, july 2, 2012, 12:00:00

I am looking for a good technique to get away from what I am tempted to do: to set a global variable.



The first time someone runs a function by clicking a button it triggers an initial function to turn a few things into draggables. Later, if they click the button a second time I want to determine if the init function has been initialized, and if so to not call it again. I could easily do this by setting a global variable from the init function and then checking that variable from the click function, but I'm wondering how to do this without setting a global variable. I would really like an example of a way to do this.


More From » jquery

 Answers
16

You could add a property to the function:



function init() {
init.called = true;
}

init();

if(init.called) {
//stuff
}

[#84510] Sunday, July 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
claudiofredye

Total Points: 583
Total Questions: 101
Total Answers: 115

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;