0

I have this code which works fine, for creating a flashing affect at the end of a timer, however Im sure this could be done more easily using a loop. Can anyone show me that loop please as I cant figure it out, thanks in advance.

function stopTimer() {
clearTimeout(timerID);
timerID = null;
timerObj.value = save;

var theBody = document.getElementById("theBody");
setTimeout(function () { theBody.className = "white"; }, 0);
setTimeout(function () { theBody.className = "red"; }, 250);
setTimeout(function () { theBody.className = "white"; }, 500);
setTimeout(function () { theBody.className = "red"; }, 750);
setTimeout(function () { theBody.className = "white"; }, 1000);
setTimeout(function () { theBody.className = "red"; }, 1250);
setTimeout(function () { theBody.className = "white"; }, 1500);
setTimeout(function () { theBody.className = "red"; }, 1750);
setTimeout(function () { theBody.className = "white"; }, 2000);
setTimeout(function () { theBody.className = "white"; }, 2250);
setTimeout(function () { theBody.className = "red"; }, 2500);
setTimeout(function () { theBody.className = "white"; }, 2750);
setTimeout(function () { theBody.className = "red"; }, 3000);
setTimeout(function () { theBody.className = "white"; }, 3250);
setTimeout(function () { theBody.className = "red"; }, 3500);
setTimeout(function () { theBody.className = "white"; }, 3750);
setTimeout(function () { theBody.className = "red"; }, 4000);
setTimeout(function () { theBody.className = "white"; }, 4250);

var audio = document.createElement("audio");
audio.src = "alarm.mp3";
theBody.appendChild(audio);

audio.setAttribute("id", "audio");
audio.play();
setTimeout(function () {
    audio.pause();
}, 6000);``

}

3 Answers 3

2

Here's a quick/dirty way to do it

function white() { theBody.className = "white"; }
function red()   { theBody.className = "red"; }

for (var i=0; i<=4250; i+=500) {
  setTimeout(white, i);
  setTimeout(red, i+250);
}

More likely though you would just want to set an interval using setInterval and continuously loop until some future event. Like

  • loop for 5 seconds,
  • or, loop until user clicks stop button
  • or, some other condition
Sign up to request clarification or add additional context in comments.

Comments

1

you can use setInterval which recalls the function in question every interval until canceled.

var myVar = setInterval(function(){ 
  theBody.className = (theBody.className == "white")? "red" : "white";
}, 250);

To stop the execution all you have to do is :

 clearInterval(myVar);

Comments

0

You can use setInterval to do something at intervals and you can use clearInterval to clear that interval (to make it stop).

var theBody = document.getElementById("theBody");
var max = 10;
var effect = setInterval(function () {
    theBody.className =  (max % 2 == 0) ? "white" : "red";
    max--;
    if (max == 0) {
        clearInterval(effect);
    }
}, 
250 // how often to do this in milliseconds
);

see fiddle

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.