Is there a way to check if a function works in jQuery?

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

+4
source share
7 answers

Set the external flag.

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

The same principle applies to jQuery functions.

+7
source

It is very simple. The function does not work.

Javascript is strictly single. If the browser responds to any user events, there is currently no function.

+7
source

If there are many functions in your call, there are many utilities that will help you cope with this promise. Promise

http://api.jquery.com/promise/

+1
source

You can set a flag. Something like that:

 (function () { // your application within a closure to not pollute global scope var handlerRunning = false; // the function that will be called and shouldn't be running twice var handler = function () { if (handlerRunning) return; handlerRunning = true; // do stuff here // when you're done, set handlerRunning to false handlerRunning = false; } // assign your code to event handler for example document.getElementById('awesome-button').addEventListener('click', handler, false); }()); 
0
source

Do not get far from your head, but you can use a variable to determine if the function is executing. sort of:

 function MyWrapper() { this.running = false; this.run = function() { if (this.running==false) { this.running = true; // do something this.running = false; return true; } else { return false; } }; } 
0
source

use jQuery.when()

If a jQuery.when is passed for jQuery.when , its method returns a Promise object (a subset of the Deferred methods). Additional methods of the Promise object can be called to attach callbacks, such as deferred.then . When Deferred resolves or rejects, usually with the code that Pending originally created, the corresponding callbacks will be called. For example, the jqXHR object returned by jQuery.ajax() is Promise and can be used as follows:

 $.when( $.ajax( "test.aspx" ) ).then(function( data, textStatus, jqXHR ) { alert( jqXHR.status ); // Alerts 200 }); 
0
source

Late answer, but still I wanted to post my answer here. I ran into a similar problem when I have a background server check function with setInterval built into it. But in my code there are many possibilities to run the same function according to some other criteria. Therefore, to make sure that I don’t think about it, I simply used local storage to ensure that the function is not called again and again.

This function is in my HTML file,

 function serverCheck(value){ localStorage.setItem("isFunctionRunning",1); setInterval(checkServerStatus, value); } 

The snippet below is in my other JS file,

 function xyz(){ //someCode var checkFunction = localStorage.getItem("isFunctionRunning"); if(checkFunction != 1){ serverCheck(frequency); } } 
0
source

Source: https://habr.com/ru/post/1401712/


All Articles