Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if a function is currently running in jQuery?

Tags:

jquery

I have a document that may give the user the ability to run the same function more than once. Is there a way to check if the function is currently running before I can reactivate it again?

like image 351
Joe Avatar asked Mar 15 '12 23:03

Joe


People also ask

How do you check if JS function is running?

Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value.

What is $( function () in jQuery?

ready(function() { }); So a jQuery function, which is prefixed with the $ or the word jQuery generally is called from within that method. $(document). ready(function() { // Assign all list items on the page to be the color red.

How do I know if I am using jQuery?

You can test if jQuery is loaded by opening your javascript console (in Chrome: Settings > More tools > Javascript console). Where the little blue arrow appears type: if(jQuery) alert('jQuery is loaded'); Press enter.

What is this () in Javascript?

“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.


1 Answers

Set an external flag.

var functionIsRunning = false;

function myFunction() {
    if (!functionIsRunning) {
        functionIsRunning = true;
        //do stuff
        functionIsRunning = false;
    }
}

The same principle applies to jQuery functions.

like image 96
pete Avatar answered Sep 28 '22 06:09

pete