pa1.Shetty
pa1.Shetty

Reputation: 421

Destroying a function in javascript before calling it again

I have created a website. While the user opens it for the first time, javascript calls( showDetails() ) a function (Which does a few tasks in the backend), I also have updated the button in my website, when the user presses it the same function will be called again ( showDetails() ) and does the same task.

But the problem here is that when the user presses the update button another instance of showDetails will be created and two instances will be executing in the background. (If user presses update 5 times 5 instances of the showDetails() function will be created).

I have tried to resolve this by assigning a function to an variable and destroying it like this:

//creating variable
var variShow = null;

//calling showdetails() when user opens the site and assigning it to variable variShow
variShow = showdetails();

//when user presses update button checking whther variShow is null or not. if not null assign it to null and call showdetails() again.
if (variShow !== null) {
    variShow = null;
}
variShow = showdetails();

But it still does not work, how can I resolve this?

Upvotes: 3

Views: 1546

Answers (2)

Nikola Lukic
Nikola Lukic

Reputation: 4244

This is solution : You can do it with flag but this is better just use override on the end !

var showDetails = function() {

   console.log("Active method!")
  // function 
  // your code here 

  // Override self
  showDetails = function(){console.log("not active any more")}  
}


// Just for test 
showDetails();

// Just for test again
showDetails();

We override function to make nothing on call (empty):

   showDetails = function(){}

Then you will avoid error on other call showDetails();

If you put :

   showDetails = null;

then this will produce 'showDetails()' error. You can make it null or

   showDetails = undefined;

but then you must check before any call :

 // This is strong check work on all old and new browsers
  if (typeof showDetails !== 'undefined' && typeof showDetails === "function") {
    showDetails()
  }

var showDetails = function() {

   console.log("Active method!")
  // function 
  // your code here 
  showDetails = undefined;
  
  // modern js
  // delete showDetails
  
}

// First time call
showDetails();


// This is strong check work on all old and new browsers
if (typeof showDetails !== 'undefined' &&
    typeof showDetails === "function") {
    // Second call must be protected/checked
    showDetails()
    
} else {
   console.log("Not active method!");
}

Upvotes: 2

Solomon Tam
Solomon Tam

Reputation: 739

var interrupted = false;

function showDetails(){

    interrupted = false;

    //Some task
    //...

    //Check if someone interrupt the function at some point of the function
    if(interrupted){
        //Some code to handle interruption here
        //...
        interrupted = false;
        return;
    }

    //Some task
    //...

    interrupted = false

}

When user enter the page

showDetails();

When user press button

interrupted = true;
var waitInterval = setInterval(function(){
    //Wait until the running function to be terminated.
    if(!interrupted){
        clearInterval(waitInterval);
        showDetails();
    }
}, 100)

Upvotes: 0

Related Questions