/    Sign up×
Community /Pin to ProfileBookmark

Disable a Javascript Function

Hello,

I have a function, and at some point, I need another function to disable it so that it no longer gets executed. Example:

[CODE]
function first() {
//…
}

function second() {
//here I need the code to disable first()
}
[/CODE]

Thank You

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — i'm not sure of what you are asking. if you meant to cancel the function's execution you can use [b]return[/b] statement. however, you can not cancel the execution of a function with another function.
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — Ok. Let me explain.

I have a couple of images. When you hover your mouse over them they change position because of first(). Then, when all the images are in their correct position, second() causes a change in all of them (margins).

Basically, after the execution of second(), I do not want first() to act on the images anymore.
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — can you provide a brief on how both function are used? perhaps a link. you can overwrite first() function with a null within second function so that first() function doesn't exist anymore. however, depending on how they are used, it may generate error. if function is called as part of an event, then the best solution would be to clear that event. otherwise, when the event occur again, you will be calling something as [b]null()[/b] and will generate an error because there is no [b]null()[/b] function.
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — Here is the part of my code which might help you understand (make sure to scroll to the right because there are many img in one line...):

[CODE]
//In my <body>, I have these images

<div id="mosaic">
<img src="_images/mosaic/mosaic_11.jpg" name="mosaic01" id="mosaic01" onmouseover="swap('mosaic01'); end();" /><img src="_images/mosaic/mosaic_03.jpg" name="mosaic02" id="mosaic02" onmouseover="swap('mosaic02'); end();" /><img src="_images/mosaic/mosaic_02.jpg" name="mosaic03" id="mosaic03" onmouseover="swap('mosaic03'); end();" /><img src="_images/mosaic/mosaic_10.jpg" name="mosaic04" id="mosaic04" onmouseover="swap('mosaic04'); end();" /><img src="_images/mosaic/mosaic_09.jpg" name="mosaic05" id="mosaic05" onmouseover="swap('mosaic05'); end();" /><img src="_images/mosaic/mosaic_16.jpg" name="mosaic06" id="mosaic06" onmouseover="swap('mosaic06'); end();" /><img src="_images/mosaic/mosaic_13.jpg" name="mosaic07" id="mosaic07" onmouseover="swap('mosaic07'); end();" /><img src="_images/mosaic/mosaic_12.jpg" name="mosaic08" id="mosaic08" onmouseover="swap('mosaic08'); end();" /><img src="_images/mosaic/mosaic_05.jpg" name="mosaic09" id="mosaic09" onmouseover="swap('mosaic09'); end();" /><img src="_images/mosaic/mosaic_04.jpg" name="mosaic10" id="mosaic10" onmouseover="swap('mosaic10'); end();" /><img src="_images/mosaic/mosaic_06.jpg" name="mosaic11" id="mosaic11" onmouseover="swap('mosaic11'); end();" /><img src="_images/mosaic/mosaic_08.jpg" name="mosaic12" id="mosaic12" onmouseover="swap('mosaic12'); end();" /><img src="_images/mosaic/mosaic_07.jpg" name="mosaic13" id="mosaic13" onmouseover="swap('mosaic13'); end();" /><img src="_images/mosaic/mosaic_15.jpg" name="mosaic14" id="mosaic14" onmouseover="swap('mosaic14'); end();" /><img src="_images/mosaic/mosaic_14.jpg" name="mosaic15" id="mosaic15" onmouseover="swap('mosaic15'); end();" /><img src="_images/mosaic/mosaic_01.jpg" name="mosaic16" id="mosaic16" onmouseover="swap('mosaic16'); end();" />
</div>


//Then, in my javascript, I have the two functions swap() and end()

function swap(n) {

if(n == "mosaic01") {
var x = document.mosaic01.src;
var y = document.mosaic06.src;
document.mosaic01.src = y;
document.mosaic06.src = x;
}

else if(n == "mosaic02") {
var x = document.mosaic02.src;
var y = document.mosaic03.src;
document.mosaic02.src = y;
document.mosaic03.src = x;
}

else if(n == "mosaic03") {
var x = document.mosaic03.src;
var y = document.mosaic02.src;
document.mosaic03.src = y;
document.mosaic02.src = x;
}

else if(n == "mosaic04") {
var x = document.mosaic04.src;
var y = document.mosaic07.src;
document.mosaic04.src = y;
document.mosaic07.src = x;
}

else if(n == "mosaic05") {
var x = document.mosaic05.src;
var y = document.mosaic09.src;
document.mosaic05.src = y;
document.mosaic09.src = x;
}

else if(n == "mosaic06") {
var x = document.mosaic06.src;
var y = document.mosaic11.src;
document.mosaic06.src = y;
document.mosaic11.src = x;
}

else if(n == "mosaic07") {
var x = document.mosaic07.src;
var y = document.mosaic10.src;
document.mosaic07.src = y;
document.mosaic10.src = x;
}

else if(n == "mosaic08") {
var x = document.mosaic08.src;
var y = document.mosaic12.src;
document.mosaic08.src = y;
document.mosaic12.src = x;
}

else if(n == "mosaic09") {
var x = document.mosaic09.src;
var y = document.mosaic05.src;
document.mosaic09.src = y;
document.mosaic05.src = x;
}

else if(n == "mosaic10") {
var x = document.mosaic10.src;
var y = document.mosaic07.src;
document.mosaic10.src = y;
document.mosaic07.src = x;
}

else if(n == "mosaic11") {
var x = document.mosaic11.src;
var y = document.mosaic06.src;
document.mosaic11.src = y;
document.mosaic06.src = x;
}

else if(n == "mosaic12") {
var x = document.mosaic12.src;
var y = document.mosaic08.src;
document.mosaic12.src = y;
document.mosaic08.src = x;
}

else if(n == "mosaic13") {
var x = document.mosaic13.src;
var y = document.mosaic10.src;
document.mosaic13.src = y;
document.mosaic10.src = x;
}

else if(n == "mosaic14") {
var x = document.mosaic14.src;
var y = document.mosaic15.src;
document.mosaic14.src = y;
document.mosaic15.src = x;
}

else if(n == "mosaic15") {
var x = document.mosaic15.src;
var y = document.mosaic14.src;
document.mosaic15.src = y;
document.mosaic14.src = x;
}

else {
var x = document.mosaic16.src;
var y = document.mosaic11.src;
document.mosaic16.src = y;
document.mosaic11.src = x;
}

}

function end() {
if((document.mosaic01.src.match(/w+.w{3,4}$/) == "mosaic_01.jpg") && (document.mosaic02.src.match(/w+.w{3,4}$/) == "mosaic_02.jpg") && (document.mosaic03.src.match(/w+.w{3,4}$/) == "mosaic_03.jpg") && (document.mosaic04.src.match(/w+.w{3,4}$/) == "mosaic_04.jpg") && (document.mosaic05.src.match(/w+.w{3,4}$/) == "mosaic_05.jpg") && (document.mosaic06.src.match(/w+.w{3,4}$/) == "mosaic_06.jpg") && (document.mosaic07.src.match(/w+.w{3,4}$/) == "mosaic_07.jpg") && (document.mosaic08.src.match(/w+.w{3,4}$/) == "mosaic_08.jpg") && (document.mosaic09.src.match(/w+.w{3,4}$/) == "mosaic_09.jpg") && (document.mosaic10.src.match(/w+.w{3,4}$/) == "mosaic_10.jpg") && (document.mosaic11.src.match(/w+.w{3,4}$/) == "mosaic_11.jpg") && (document.mosaic12.src.match(/w+.w{3,4}$/) == "mosaic_12.jpg") && (document.mosaic13.src.match(/w+.w{3,4}$/) == "mosaic_13.jpg") && (document.mosaic14.src.match(/w+.w{3,4}$/) == "mosaic_14.jpg") && (document.mosaic15.src.match(/w+.w{3,4}$/) == "mosaic_15.jpg") && (document.mosaic16.src.match(/w+.w{3,4}$/) == "mosaic_16.jpg")) {
document.getElementById("mosaic01").style.margin = "0px";
document.getElementById("mosaic02").style.margin = "0px";
document.getElementById("mosaic03").style.margin = "0px";
document.getElementById("mosaic04").style.margin = "0px";
document.getElementById("mosaic05").style.margin = "0px";
document.getElementById("mosaic06").style.margin = "0px";
document.getElementById("mosaic07").style.margin = "0px";
document.getElementById("mosaic08").style.margin = "0px";
document.getElementById("mosaic09").style.margin = "0px";
document.getElementById("mosaic10").style.margin = "0px";
document.getElementById("mosaic11").style.margin = "0px";
document.getElementById("mosaic12").style.margin = "0px";
document.getElementById("mosaic13").style.margin = "0px";
document.getElementById("mosaic14").style.margin = "0px";
document.getElementById("mosaic15").style.margin = "0px";
document.getElementById("mosaic16").style.margin = "0px";
}
}
[/CODE]


What I need is that end() stops swap() from being executed because I do not want the images to swap anymore once end() has been executed.
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — add the following line to the very end of [b]swap[/b] function. check if that is what you were looking for:
<i>
</i>document[n].onmouseover = null;

the image wouldn't swap anymore after the first mouse over.
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — No, sorry. This is not what I want, although you were close...

I need to make the images stop moving AFTER end() has executed.
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — i'm a bit confused. ?

you want to also prevent the function [b]swap[/b] being called from those image that are related? that is, mosaic01 and mosaic06 are related. so, once a mouse over occur on one of them, you want to cancel the swap in both images, independently on which of them occured. that is?
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — Oh never mind. I found how to do it! Here is what I added:

[CODE]
//I made this a global variable
var s;

//Then at the end of the if statement in my end() function, I added this
s = 1;

//And at the beginning of my swap() function I added this

if(s == 1)
return 0;
[/CODE]


So now, my swap() function does not get executed after end() has been executed. ?
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — so, you wanted to swap only one image.
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — no...I don't think you understand my code very well...maybe you should review my code, I am sure you will understand. Make sure to use the horizontal scroll bar when reading my code because there are many images on the same line. Maybe you missed some.

I don't know how to explain more than this. Try to reread my code and you will probably understand. Anyways, it works now so thank you!
Copy linkTweet thisAlerts:
@ZeroKilledApr 25.2009 — has read the code from the very first time and i understood the code. after the first call to [b]swap[/b] function, it will not be called anymore. however, [b]end[/b] function always is called. so, my conclusion was to swap only one image (no matter which). at least that is the effect i can see.
Copy linkTweet thisAlerts:
@jpfaletauthorApr 25.2009 — No. This is why you are wrong. You got it reversed. swap() is called EVERYTIME, and end() is called only once.
×

Success!

Help @jpfalet spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...