set and clearInterval with if else

This is a discussion on "set and clearInterval with if else" within the JavaScript Forum section. This forum, and the thread "set and clearInterval with if else are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 10th, 2007, 20:26
Junior Member
Join Date: Apr 2007
Location: uk
Age: 20
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
set and clearInterval with if else

Can someone please tell me why the clearInterval() func is never called even though the var pl certainly hits 10.
Wierd
Code: Select all
 
<script type="text/javascript">
var flashObj = document.getElementById("gameJS"); 
var pl = 0;
 function showPercent(){
  
          pl++;
  
 }
if(pl == 10){
 
clearInterval();
}
else {
 setInterval("showPercent()",1000);
}
</script>
Reply With Quote

  #2 (permalink)  
Old Oct 10th, 2007, 20:31
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: set and clearInterval with if else

Code: Select all
 
<script type="text/javascript">
var flashObj = document.getElementById("gameJS"); 
var pl = 0;
 function showPercent(){
  
          pl++;
  
if(pl == 10){
 
clearInterval();
}
else {
 setInterval("showPercent()",1000);
}
 }</script>
Try that.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #3 (permalink)  
Old Oct 11th, 2007, 05:35
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: set and clearInterval with if else

setInterval will run over and over by itself, there is no need to call it repeatedly. It is also good to store the reference to a timeout or interval in a variable.

Try this

Code: Select all
 
 
<script type="text/javascript">
var flashObj = document.getElementById("gameJS"); 
var pl = 0;
var timeout = false;
 function showPercent(){
  
          pl++;
  
if(pl == 10){
 
clearInterval(timeout);
}
else {
 timeout = setTimeout("showPercent()",1000);
}
 }</script>
The way your script was working had 10 different intervals running but was only clearing one of them.

This is a timeout so it runs only once.

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 19:18.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43