javascript - Can't use cancelAnimationFrame. Nothing happens (chrome) -
problem: can't cancel requestanimationframe using chromes recognized cancelanimationframe.
what tried: used console.log no errors displayed. console.logged cancelanimationframe , undefined, while request counterpart logs fine. tried different version of cancelanimationframe, including prefixed versions:
cancelanimationframe webkitcancelanimationframe window.cancelanimationframe etc..
i tried putting canceling code function expected doesn't matter.
question: why can't stop requestanimationframe? canceling way recommended?
http://jsfiddle.net/p8hv15j4/2/
var elem = document.getelementbyid('test'); var currentpos = 0; var requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe; function fn() { currentpos += 5; elem.style.left = currentpos + "px"; if (math.abs(currentpos) >= 900) { currentpos = -500; } requestanimationframe(fn); } fn(); settimeout(function() { cancelanimationframe(fn); }, 1000)
requestanimationframe
returns id, need pass cancelanimationframe
.
in fact, works same way set/cleartimeout
- setting returns identifier, , cancel identifier.
Comments
Post a Comment