How Can Use The Setinterval() To Change The Text?
I've been working at this for quite some time now. I'm trying to create a simple timer that counts down. I've gotten the alert() to work, but unfortunately the textToChange does no
Solution 1:
You are using a changingText
function and a changingText
array ...
See the conflict ?
As additional info,
do not use strings as name functions in the setInterval
method. Use a reference to the method directly
setInterval(changingText, 1000);
Solution 2:
For a simple countdown timer, this code is too big. Basically, what you should be doing is:
- Set the number of seconds from which should be counted down
- Start a timer using
setInterval
which decrements the counter and displays it
That can be simple as:
var countFrom;
var timer;
functionrun_timer() {
document.getElementById("textToChange").innerHTML = --countFrom;
// !0 evaluates to true, if the counter reaches 0 (wait 0 seconds) ...if (!countFrom) {
clearInterval(timer);
alert("Timer finished!");
}
}
functioninit_timer () {
// stop previous timers if anyclearInterval(timer);
// start counting from 30 seconds
countFrom = 30;
timer = setInterval(run_timer, 1000);
}
Solution 3:
Your version in a fiddle. I changed the function name
Post a Comment for "How Can Use The Setinterval() To Change The Text?"