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 kn开发者_开发知识库ow I could save a value with jQuery.data() and simply remove/reset it when the plugin closes but is there another or smarter way?
typeof $.fn.my_func !== "undefined"
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;
}
}
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?
精彩评论