jQuery count-- until a specific condition is met
I have explained in a comment where and what I want the code below to do:
$("#run").click(function(){
maxspeed = 1.00;
if (maxspeed > 0.07)
{
//This is where I want the max speed to countdown at
//a decrement of -0.01 until it reaches 0.07
}
开发者_Go百科 else
{
delay(3000);
}
//Code continues below, I will edit post if required
My max speed is already stored in a variable with the value of 0.07. Any help would be appreciated, thanks.
[EDIT] Sorry, I should have explained better. I am pretty new to JS/jQuery and I have no idea how to use a count. I want my function to count down until it reaches a certain value.
Did you try : setTimeout()
?
http://www.w3schools.com/jsref/met_win_settimeout.asp
You should use a TimeInterval and start decreasing maxspeed by 0.07
maxspeed -= 0.07;
If you use a loop the action will be instant.
try this
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function(){
var maxspeed = 1.00;
var decrement = 0.01;
counter(maxspeed,decrement);
});
function counter(maxspeed,decrement)
{
while(maxspeed > parseFloat(0.07))
{
maxspeed = parseFloat(maxspeed) - parseFloat(decrement);
alert(maxspeed);
setTimeout(counter(maxspeed,decrement),3000);
}
}
</script>
精彩评论