stop javascript countdown?

  • jannin
  • Beginner
  • Beginner
  • User avatar
  • Posts: 40
  • Loc: Cranbrook, BC Canada

Post 3+ Months Ago

Hey all,
I have a script working so that if the viewer puts their cursor over an image, a countdown starts, and I'd just like to know if there's a way/how to stop the countdown if they move off the image (onMouseOut). I just need to be able to stop the function somehow, and be able to restart it again when they move back onto the image.

It doesn't matter to me whether the countdown restarts or just continues from where it was before they moved off the image...

Thanks!
/jannin
  • jannin
  • Beginner
  • Beginner
  • User avatar
  • Posts: 40
  • Loc: Cranbrook, BC Canada

Post 3+ Months Ago

:oops: never mind, I've figured out something that works. I can't restart the timer but it's for an annoying game that I'm pretty much inventing to bug my site users, so they'll just have to not try to cheat by moving off the image.

Here's how I kind of, probably unconventionally "fixed" it, by the way:

head section:
Code: [ Select ]
function Countdown(){
    if(a!=0){
    a = a-b
    //next prints the countdown:
    document.frm3.clock3.value = a
    setTimeout("Countdown()", 1000)
    //next just starts another timer... it's kind of an odd game:
    }else if(a==0){
    startClock()
    }
}
//this is my unconventional stop the countdown function:
function Stopdown(){
    b=0
}
  1. function Countdown(){
  2.     if(a!=0){
  3.     a = a-b
  4.     //next prints the countdown:
  5.     document.frm3.clock3.value = a
  6.     setTimeout("Countdown()", 1000)
  7.     //next just starts another timer... it's kind of an odd game:
  8.     }else if(a==0){
  9.     startClock()
  10.     }
  11. }
  12. //this is my unconventional stop the countdown function:
  13. function Stopdown(){
  14.     b=0
  15. }


body:
Code: [ Select ]
//MouseOver starts it, MouseOut stops it.
<a href="#" onMouseOver="Countdown()" onMouseOut="Stopdown()">
<img src="...source...">
</a>
  1. //MouseOver starts it, MouseOut stops it.
  2. <a href="#" onMouseOver="Countdown()" onMouseOut="Stopdown()">
  3. <img src="...source...">
  4. </a>


There's more, of course, but this is the only section that really does anything. And again, probably the least proper way to do it, but if it works, I'll take it.

/jannin
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

I see the script uses setTimeout. In this case, you will probably be able to use clearTimeout to stop running your script...

For example, you need to assign an ID to your setTimeout:
Code: [ Select ]
myTimer = setTimeout("Countdown()", 1000);


Then you can use that ID to target your setTimeout and call a clearTimeout
Code: [ Select ]
clearTimeout(myTimer);


I didn't test it, but it should work.
  • jannin
  • Beginner
  • Beginner
  • User avatar
  • Posts: 40
  • Loc: Cranbrook, BC Canada

Post 3+ Months Ago

Thanks lostinbeta,
works beautifully.
I knew there had to be a proper way to do it, I just got impatient and started fooling around with it instead.

Thanks again!
/jannin
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Glad I could help :)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 129 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.