'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.



Solution 1:[1]

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

Solution 2:[2]

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)
}

Solution 3:[3]

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);
}

Solution 4:[4]

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 :-)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 4b0
Solution 3 Joss Classey
Solution 4 Sylvio Ruiz Neto