PDA

View Full Version : changing operations of a function


chrismiceli
12-01-2002, 08:59 PM
is there a way to change the operations of a function like this

function hello() {
alert("hi")
}
//change to this
function hello() {
alert("hello")
}

thanx in advanced.

beetle
12-01-2002, 10:03 PM
You mean parameters?function hello(msg) {
alert(msg)
}

hello("hi");
hello("hello");

chrismiceli
12-02-2002, 12:15 AM
i want to change the operations to nothing, because i have a timer, and i want to stop it (making the funcion do nothing). like this
time = 0;
function timer() {
document.getElementById("spanname").innerHTML = time;
++time;
setTimeOut("timer()", 1000);
}
i want it to do nothing after i tell it to.

jkd
12-02-2002, 12:38 AM
You can always overwrite it by simply redeclaring it. A function is just a datatype assigned to a variable in Javascript anyway.

Your example should work, and if it doesn't, use anonymous function notation:

function hello() {
alert("hi")
}

// overwrite it
hello = function() { alert("hello") }

chrismiceli
12-02-2002, 01:06 AM
thanx jkd, you rule.:thumbsup:

beetle
12-02-2002, 01:26 AM
That certainly works. I'd like to note, however, that timeouts can be cancelled, or cleared, if assigned to a variable.function timer() {
document.getElementById("spanname").innerHTML = time;
++time;
var myTimer = setTimeout("timer()", 1000);
}

timer(); //call function
clearTimeout(myTimer); //cancel timeoutThis is the more typical method used for something like a timer

chrismiceli
12-02-2002, 02:38 AM
i know, just didn't think of it at the time, but both ways work, thanx for the input.

whammy
12-02-2002, 02:39 AM
Good point, beetle, declaring a variable for the setTimeout() solved a problem I had a couple of months ago. ;)