See this fiddle: http://jsfiddle.net/ym1aLk25/9/
var s = $('#shake');
var randomTran = function (flag) {
flag = flag || 0;
if (flag < 6) {
var rh = Math.floor((Math.random() * 10) - 5),
rv = Math.floor((Math.random() * 10) - 5);
s.transit({x: rh,y: rv}, 50, randomTran.bind(this, ++flag))
};
};
randomTran();
s.transit({x: 0,y: 0});
I'm trying to make the element shake for a few seconds and then return to its original position. But it doesn't work as I expected, the problem is with the callback function. According to this question: If a jQuery function calls itself in its completion callback, is that a recursive danger to the stack?, it seems that while callback is still looping, the codes that come after the callback function are also being executed. So how to achieve my goal, other than setting a timeout? And where can I find a more detailed explanation about this mechanism?