View Single Post
  #3 (permalink)  
Old Oct 11th, 2007, 05:35
Rakuli's Avatar
Rakuli Rakuli is offline
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,
Reply With Quote