Sasith
Sasith

Reputation: 790

stop d3js globe auto rotate

I have a d3js globe and I found code to auto rotate it but I also need the code for stop the rotate. Any one can help? Here is the code.

var rotate = [.001, 0],
velocity = [.013, 0],
time = Date.now();

 function rotateGlobe() {  
 d3.timer(function() {
    var dt = Date.now() - time;
    projection.rotate([rotate[0] + velocity[0] * dt, rotate[1] + velocity[1] * dt]);
    redraw();
});
}



$(document).ready(function(){

  $("#stop").click(function(){
     stopGlobe()
   }); 

  function stopGlobe() {  
     //need stop code here
   }
}); 

Upvotes: 0

Views: 696

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

You can do something like this to stop the timer timer.stop();:

var rotate = [.001, 0],
velocity = [.013, 0],
time = Date.now();
var timer;//make timer global.
 function rotateGlobe() {  
 timer = d3.timer(function() {
    var dt = Date.now() - time;
    projection.rotate([rotate[0] + velocity[0] * dt, rotate[1] + velocity[1] * dt]);
    redraw();
});
}



$(document).ready(function(){

  $("#stop").click(function(){
     stopGlobe()
   }); 

  function stopGlobe() {  
    timer.stop();
   }
}); 

Upvotes: 3

Related Questions