0

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.

5
  • 5
    abc() isn't a loop or interval, so I don't think you are actually trying to stop running the function. It seems like you want to undo the changes made by the function. Is that correct? Commented Jul 25, 2019 at 6:05
  • set abc = function(){} when you dont need abc to be called. Technically abc will be called, but since it has empty definition, nothing will work Commented Jul 25, 2019 at 6:06
  • hard to tell what is your expected result, to be honest, but destroying the function seems to be a bad approach Commented Jul 25, 2019 at 6:19
  • 1
    @sinaraheneba Yes, I want to undo changes. Can you please help? Commented Jul 25, 2019 at 6:32
  • Undoing the changes made by a function, removing the function itself (e.g., deleting it), and stopping a looping/repeating function from executing again are all separate tasks. You will need to make a function that undoes the changes made by abc(), or adjust abc() so that it undoes changes after they're not needed; or perhaps, depending on your app, it would be easiest to reload the content without running abc() on it. Commented Jul 25, 2019 at 6:40

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

delete window.abc would be cleaner still.
I tried it in my browser to be sure. :) Maybe it only works because you use it on window.abc rather than just abc.
Yes i did indeed, but for you it worked? Mozilla Docs: "Functions which are part of an object (apart from the global scope) can be deleted with delete." (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)
Using Chrome. This is what I see. Did you define it using var maybe?
0

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

2 Comments

I guess he wants something like phps unset function. And why he should do this? He could write the else block directly into the refresh function
Nothing personal. Don't guess. Give option to OP , choice what ever want.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.