1
start.on('click', function(){
  setInterval(time, 1000);

  object.animate({ marginTop: '500px' }, 3000, function () {
    normaal.fadeIn(700);
  });
});


function time() {
  change.textContent = Number(change.textContent) + 50;
}

How excatly can I stop (clear) this setInterval. I want it to automatically start after 3000 miliseconds once the even has been called.

Mindsers
  • 662
  • 8
  • 25
user7548524
  • 53
  • 1
  • 1
  • 6

3 Answers3

2

You can make use of the clearInterval method.

var intervalID = setInterval(time, 1000);

The returned value of setInterval is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to WindowOrWorkerGlobalScope.clearInterval() to cancel the timeout.

and then to clearInterval, pass the intervalID like clearInterval(intervalID);

Also, setInterval calls the callback repeatedly, if you need to execute a code block only once then use setTimeout instead.

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
1

setInterval keeps calling the method over and over. What you want is to use setTimeout instead, which will call the method once (and only once) after waiting the specified period of time. You can use clearTimeout if you want to prevent a setTimeout call from executing.

setTimeout(time, 3000);
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • Thank you! Do you also happen to know how can make the animation go faster by the second. Just like gravity I want the animation to speed up with time. – user7548524 Feb 13 '17 at 22:28
  • You should ask that as a separate question since then you'll get more/better answers. I can't say I know too much about animation, sorry. – Matthew Herbst Feb 13 '17 at 22:32
-1

var interval;

start.on('click', function () {
  interval = setInterval(time, 1000);

  object.animate({ marginTop:'500px' }, 3000, function () {
    normaal.fadeIn(700);
  });
});


function time() {
  change.textContent = Number(change.textContent) + 50;
}

function stop() {
  clearInterval(interval);
}
Mindsers
  • 662
  • 8
  • 25
Malo Guertin
  • 307
  • 2
  • 16