Humesh Sindpure
Humesh Sindpure

Reputation: 69

how to destroy a function in another function javascript?

Function:

function abc()
{
  $('#table_id tr td').removeClass('highlight'); 
  $(this).addClass('highlight'); 
  tableText($table_label,$table_id);
}

abc();


function  refresh() 
{            
  abc().hide; // Need help at this.
}

<button class="refresh" onclick="refresh()">Refresh</button>

I'm trying to remove function/stop running abc() function, when refresh button was clicked.

Upvotes: 0

Views: 2282

Answers (4)

Sylvio Ruiz Neto
Sylvio Ruiz Neto

Reputation: 192

Wrap the function inside an object to delete it. You can use the window object or create a new one. Example:

const functions = new Object;
functions.abc = () => { /* do something */ };
functions.abc();
const refresh = () => {
  if ('abc' in functions){ functions.abc(); delete functions.abc; }
  else { /* will do nothing */ }
};

Solved :-)

Upvotes: 0

NoNickAvailable
NoNickAvailable

Reputation: 398

Try this code, if abc is in the global scope:

window.abc = function() {
  return false;
}

or you could do: window.abc = undefined when it's in the global scope.

when it's a method: delete obj.abc

Upvotes: 1

Joss Classey
Joss Classey

Reputation: 1062

Put all the code you only want to run once at the start inside window.onload

window.onload = function() {
  $('#table_id tr td').removeClass('highlight'); 
  $(this).addClass('highlight'); 
  tableText($table_label,$table_id);
}

Upvotes: 0

4b0
4b0

Reputation: 22323

You can pass param in your function and check condition like below.

function abc(arg) {
  if (arg) {
    $('#table_id tr td').removeClass('highlight');
    $(this).addClass('highlight');
    tableText($table_label, $table_id);
  } else {
    //Do what ever you want if needed
  }
}

abc(true);

function refresh() {
  abc(false)
}

Upvotes: 0

Related Questions