1

I am having a problem where I want to stop a particular function from executing. This function is located on another plugin which I can't change the core files so I am wondering if I can stop a specific function from that file from executing?

So for example that function is testFunction(); and I want to stop that later in the code maybe in on document ready...etc.

Thanks!

2
  • I've wrestled with this before... and lost. I'm sure there is some sort of hack but I think it would generally be bad form to do this. Commented Jun 6, 2011 at 16:59
  • You should provide a bit more information. Some parts may be overridden. Other may prove to be more difficult. Of course causing an parse error will probably stop it from executing ;) Commented Jun 6, 2011 at 17:09

6 Answers 6

1

Is the function public (as opposed to private via a closure)? And, does it need to be operational at all for things to work, or can you chop it out in total and be fine? If the latter, you can replace the function with a new one:

otherLibrary.testFunction = function(){};

If you want to disable it for a temporary amount of time, you can store the function in a temporary variable, and restore it later:

var removedFunc = otherLibrary.testFunction;
otherLibrary.testFunction = function(){};

// do something, time passes, whatever...

otherLibrary.testFunction = removedFunc;

Or, if you want to be able to toggle it, a slight variation:

var removedFunc = otherLibrary.testFunction;
var testFunctionEnabled = true;
otherLibrary.textFunction = function(){
    if(testFunctionEnabled){
        removedFunc.call(this, arguments);
    }
};

And then just set testFunctionEnabled as you need to.

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

4 Comments

Its a public function loaded on dom ready... No it will not break anything if I remove that function... So should I use your first example?
@Rick The first function is the quick hack and slash way, will get the job done if that function is never needed. Does it execute this function on dom-ready as well?
yes the function i want to stop is being called on dom-ready. But take note that i have no access to that file...So does that mean I just need to run another dom-ready and make sure it is AFTER the function i want to stop?
@Rick actually, if you want to stop it from running, you'd want to make this change before, not after. I'm not sure how you're detecting domReady state, and if domReady events are guaranteed to execute in order. However, you actually don't need to wait on the dom to be ready, just for the third party lib to load. If you have a <script> tag for the lib, then right after do your own <script> tag to redefine the function.
0

you should try to extend the function before you call it e.g.

$.extend(plugin.function, myNewFunction);

also, check the plugin API to see if you can pass a paramter to override the function or actually access the api of the plugin e.g.

$('#mytip').api('hover', myHoverFunction);

Comments

0

If you have access to the object which refers to the "testFunction" function then you could replace it with an empty function, e.g.:

SomePlugin.testFunction = function() { };

Ultimately, if you don't have a way to overwrite that symbol then there's not much you can do to stop the method from running (unless you can provide more details).

Comments

0

If that function schedules itself with setInterval, you can use clearInterval(thefunction) to prevent it from being called again. But if it's a loop, I don't know if that's possible.

Comments

0

Simply set the function to null.

JavaScript example:

function write() {
    el.innerHTML += 'Lorem ipsum dolor sit amet.<br />';
    if (execute != null) execute();
}
function execute() {
    setTimeout(function() { write() }, 500);
}
function abort() {
    execute = null;
}
window.onload = function() {
    el = document.getElementById('blah');
    execute();
}

HTML:

<button type="button" onclick="abort()">Abort</button>
<p id="blah"></p>

Also you can change the following to implement stop/start. For this, assign the execute function to a variable so that you can later assign that variable back to execute.

var oldfunc = execute;
function abort() {
    if (execute == null) {
        execute = oldfunc;
        execute();
    } else {
        execute = null;
    }
    alert(execute);//shows the function code
}

2 Comments

If you set a function to null, and it gets called (which, it likely will, since it's part of a 3rd party lib), then you'll end up getting a JS error and likely everything else on the page dependent on JS will no longer execute (unless 3rd party lib handles the exception)
@Matt: I know in that case all JS will no longer execute. I found this out when I set setTimeout to the execute function itself. Maybe this is not the design that the OP wants, but I don't see a reason for downvoting, because in my script I made sure that the execute function isn't being called after it has been set to null. So there will be no errors.
0

You can overwite any method you have access to- but if the code in your module calls it internally you better give it an appropriate return.

document.write=function(){return true};
alert(document.write('tell me more!'))

Comments

Your Answer

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