Bruno
Bruno

Reputation: 362

JQuery: Check if jquery plugin is already called

I have build a jQuery plugin who needs to be 'closed' before it can be called again, so I need to check if the jQuery plugin is called (active). I know I could save a value with jQuery.data() and simply remove/reset it when the plugin closes but is there another or smarter way?

Upvotes: 3

Views: 1493

Answers (3)

Robin Maben
Robin Maben

Reputation: 23034

Hmmm, interesting case. You can consider using jQuery's queues..

var n = $('someSpecialDiv').queue("specialQue");

$('someSpecialDiv').queue("specialQue", function () {
    $.myPlugin();  //Always run your plugin(only this plugin) after queueing to this queue
  });

  alert(n.length); //This would tell you if your function is in execution if > 0

I'm not sure whether this helps your case?

Upvotes: 0

John Strickler
John Strickler

Reputation: 25421

typeof $.fn.my_func !== "undefined"

Upvotes: 1

RobertPitt
RobertPitt

Reputation: 57258

if you want to know if the plugin is active use a global variable like so:

$.fn.my_func(params)
{
    if(window.my_func_active)
    {
        console.log('Cannot run plugin');
        return;
    }

    window.my_func_active = true;
    //blah
    endme = function()
    {
       //shutdown here
       delete window.my_func_active;
    }
}

Upvotes: 0

Related Questions