Gabriel B
Gabriel B

Reputation: 21

How to stop a jQuery rotate animation?

I'm working on a audio multimedia player (turntable) based on jplayer plugin, and I'm using the rotate property on a div :

setInterval(    
    function () {
        $('#plateau').animate({
            rotate: '+=4deg'
        }, 0);
    },
    3);

Firstly, I would like to stop the rotation when a user clicks on another div.

Secondly, I would like to stop the rotation whith a maximum degree limitation.

Thank you very much.

Upvotes: 2

Views: 3903

Answers (4)

halfpastfour.am
halfpastfour.am

Reputation: 5923

Give the timeout a var!

var timer = setTimeout(function(){ ... }, 0);

and when you need to stop it:

clearTimeout( timer );

Upvotes: 1

Gabriel B
Gabriel B

Reputation: 21

If it's possible, I search a function who return the actual position in degree of the div, for can sync the movement of the arm with the actual playing position like on a real turntable player :

function rotation_bras(bras_rotation)
{
    /*var temps_actuel = $.jPlayer.convertTime(tt);*/ // Send the total time of the track
    // Begin position in degree 17deg
    // Maximum position in degree 40 deg
    // When a track is finish, the arm's turntable comeback at the initial position
    // The degree decalage should be depend to the total time of the track
    if(bras_rotation==true)
    {
        BrasTourne = setInterval(function(){$('#bras').animate({rotate: '+=0.1deg'}, 0);});
    }
    else {
        clearInterval(BrasTourne);
        $('#bras').stop();
    }
}

Thanks again

Upvotes: 0

Naftali
Naftali

Reputation: 146302

well for one thing set setInterval equal to a var:

var timer = setInterval(...)

than you can stop it with:

clearInterval(timer)

and then to stop the animation do:

$('#plateau').stop();

Upvotes: 5

Xavier
Xavier

Reputation: 1574

setInterval is returning a value. Store it, and then call clearInterval with this variable as a parameter

toto = setInterval(function(){…})
…
clearInterval(toto)

clearInterval will stop the regular execution of the function

Upvotes: 0

Related Questions