How to determine if a function has been called without setting global variable

You could add a property to the function:

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

init();

if(init.called) {
    //stuff
}

While @Levi's answer ought to work just fine, I would like to present another option. You would over write the init function to do nothing once it has been called.

var init = function () {
   // do the initializing

    init = function() {
        return false;
    }
};

The function when called the first time will do the init. It will then immediately overwrite itself to return false the next time its called. The second time the function is called, the function body will only contain return false.

For more reading: http://www.ericfeminella.com/blog/2011/11/19/function-overwriting-in-javascript/


Function.prototype.fired = false;

function myfunc() { 
    myfunc.fired = true; 
    // your stuff
};

console.log(myfunc.fired) // false

myfunc();

console.log(myfunc.fired) // true

Why don't you just check to see if your draggables have a class of draggable on them?

if ($('.mydiv').is('.draggable')) {
     //do something
}