1

When a user clicks a button, it will change the var speed value. How can i do this? At the moment, its set to 1000 ms // 1 second, shown on this line:

var increment = setInterval(increment,1000); // gain 1 ever second // THIS IS WHAT I WANT TO CHANGE

I want to change that 1000 to 2000 onClick of a button. I think its called the increment value? I just know its the speed in which i set it to gain a number in that time of 1000ms.

Code Link:

http://pastebin.com/UPaT9n3F

Jesse
  • 3,522
  • 6
  • 25
  • 40
user3295521
  • 23
  • 1
  • 1
  • 6
  • Put your code in the question. – bjb568 Feb 11 '14 at 03:38
  • possible duplicate of [Changing the interval of SetInterval while it's running](http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running) – bjb568 Feb 11 '14 at 03:39

2 Answers2

0

I'd change it to use setTimeout. So:

var increment = setInterval(increment,1000); // gain 1 ever second // THIS IS WHAT I WANT TO CHANGE
function increment(){
    Coal = Coal % 99999999999999999999 + CoalRate;
    totalCoal = totalCoal % 99999999999999999999 + CoalRate;
    document.getElementById("Coalcounter").innerHTML="Coal : " + Math.round(Coal);
    document.getElementById("CoalClickercost").innerHTML="Upgrade Coal Clicker : " + Math.round(CoalClickerPrice);
    document.getElementById("CoalRatecost").innerHTML="Upgrade Coal Rate : " + Math.round(CoalRatePrice);
}

Would become:

var interval = 1000
function increment(){
    Coal = Coal % 99999999999999999999 + CoalRate;
    totalCoal = totalCoal % 99999999999999999999 + CoalRate;
    document.getElementById("Coalcounter").innerHTML="Coal : " + Math.round(Coal);
    document.getElementById("CoalClickercost").innerHTML="Upgrade Coal Clicker : " + Math.round(CoalClickerPrice);
    document.getElementById("CoalRatecost").innerHTML="Upgrade Coal Rate : " + Math.round(CoalRatePrice);
    window.setTimeout(interval)
}
increment()

Then your onclick becomes as simple as something like..

<button onclick="swapInterval">Swap</button>

and

function swapInterval() {
    if(interval==1000) interval = 2000
    else interval = 1000
}

Note that this won't change it for the current loop (e.g. if you're on 2000, you push the button after 500ms, you'll still need to wait another 1500ms before it triggers).

SpoonNZ
  • 3,780
  • 1
  • 20
  • 25
  • BTW, it's worth pointing out, that naming a variable and a function the same thing in JavaScript isn't going to work out well for you. – SpoonNZ Feb 11 '14 at 03:45
0

you can clear the interval and create it again

$('#element').click(function(){
     window.clearInterval(incrementID); //
     timing = timing + 1000; //this is if you plan to keep adding up.
     incrementID = setInterval(increment, timing);// if not you can always hardcode 2000
});

also:

var timing = 1000;
var incrementID = setInterval(increment,timing); // gain 1 ever second // THIS IS WHAT I WANT TO CHANGE
//you should change the varname so that you can use the handle to clear the already set interval
Viktor Justo
  • 203
  • 2
  • 9
  • I have used your method but sadly, i'm still new to JavaScript and have little understanding of it. I tested it after implementing it in my code but it does not work meaning i'm doing something wrong i can not see. I am providing 2 sets of rates. One is Coal, other is Iron. The first code i provided here: http://pastebin.com/UPaT9n3F has a really similar code as the Iron one which is why i changed the var names to incrementCoal and incrementIron. I don't know if that is bad but i need it to work. New code here: http://pastebin.com/vQ4yVA1C – user3295521 Feb 11 '14 at 21:40