Reputation: 75
var dropSpeed = 3750;
var spawnSpeed = 2000;
var i = 0;
function makeSymbol(sym)
{
setInterval(function(){
if(dropSpeed > 1500){dropSpeed = dropSpeed - 350;}if(spawnSpeed > 50){spawnSpeed = spawnSpeed - 231;}
var aSymbol = sym.createChildSymbol("aSymbol" ,"stage");
i = i + 1;
symbols[i] = aSymbol.getSymbolElement();
symbols[i].animate({top: "950px"},dropSpeed); //uses the changed dropspeed
},spawnSpeed); //does not use the change spawnSpeed but keeps using the 2000
}
The spawnSpeed doesn't change but the dropSpeed does. how can i let the spawnspeed change? I hope it's not a stupid question but i'm stuck here for ages. you guys are my only hope. :)
Upvotes: 1
Views: 48
Reputation: 5978
You should use either setTimeout or clear the interval and try to run it again. setInterval registers an interval with a specific id and timespan. That's the one, the first with the 2 seconds time span. I would prefer to use setTimeout to get over the issue. Here is a plunk, you can see the code and the results. Plunk here
function makeSymbol(sym)
{
var test = function (){
if(dropSpeed > 1500){dropSpeed = dropSpeed - 350;}
if(spawnSpeed > 50){spawnSpeed = spawnSpeed - 231;}
if(spawnSpeed <= 4) spawnSpeed = 2000;
$("#speed").text((new Date()));
setTimeout(test, spawnSpeed);
}
setTimeout(test,spawnSpeed);
}
Upvotes: 0
Reputation: 4896
You are trying to change the time interval dynamically. setInterval
is suitable for static time intervals. Instead setTimeout
function can be used for this.
Try something like this.
var dropSpeed = 3750;
var spawnSpeed = 2000;
var i = 0;
function makeSymbol(sym)
{
setTimeout(function(){
if(dropSpeed > 1500){dropSpeed = dropSpeed - 350;}if(spawnSpeed > 50){spawnSpeed = spawnSpeed - 231;}
var aSymbol = sym.createChildSymbol("aSymbol" ,"stage");
i = i + 1;
symbols[i] = aSymbol.getSymbolElement();
symbols[i].animate({top: "950px"},dropSpeed); //uses the changed dropspeed
makeSymbol(sym);
},spawnSpeed); //does not use the change spawnSpeed but keeps using the 2000
}
Upvotes: 1