[此代码不是我的代码- http://www.isaacsukin.com/news/2015/01/detailed-explanation-javascript-game-loops-and-timing ] //....函数stop(){运行=假;开始=假;cancelAnimationFrame(frameID);}//...函数mainLoop(timestamp){//限制帧频.if(timestamp< lastFrameTimeMs +(1000/maxFPS)){frameID = requestAnimationFrame(mainLoop);返回;}增量+ =时间戳-lastFrameTimeMs;lastFrameTimeMs =时间戳;begin(timestamp,delta);如果(时间戳> lastFpsUpdate + 1000){fps = 0.25 *帧此秒+ 0.75 * fps;lastFpsUpdate =时间戳;frameThisSecond = 0;}frameThisSecond ++;var numUpdateSteps = 0;while(delta> = timestep){更新(时间步长);增量-=时间步长;如果(++ numUpdateSteps> = 240){恐慌();休息;}}绘制(增量/时间步长);T.textContent =时间戳;如果(时间戳> = 6000.0){T.textContent =已停止!";停止();}结束(fps);frameID = requestAnimationFrame(mainLoop);}//... cancelAnimationFrame函数不会停止动画循环.有什么建议吗?我已经挠了很长时间,请提出任何建议.解决方案满足 stop()的条件时,将调用 stop(),但代码将继续因此将需要一个新的皇家空军.只需在stop调用后添加一个返回值以防止这种情况发生(或使用 else ): ...如果(时间戳> = 6000.0){T.textContent =已停止!";停止();//stop()只是此处的子例程,在被调用后将继续返回;//< ---这里}... 修改过的小提琴 Refer to this fiddle - http://jsfiddle.net/rnqLfz14/28/[ This code is not mine - http://www.isaacsukin.com/news/2015/01/detailed-explanation-javascript-game-loops-and-timing ]//....function stop() { running = false; started = false; cancelAnimationFrame(frameID);}//...function mainLoop(timestamp) { // Throttle the frame rate. if (timestamp < lastFrameTimeMs + (1000 / maxFPS)) { frameID = requestAnimationFrame(mainLoop); return; } delta += timestamp - lastFrameTimeMs; lastFrameTimeMs = timestamp; begin(timestamp, delta); if (timestamp > lastFpsUpdate + 1000) { fps = 0.25 * framesThisSecond + 0.75 * fps; lastFpsUpdate = timestamp; framesThisSecond = 0; } framesThisSecond++; var numUpdateSteps = 0; while (delta >= timestep) { update(timestep); delta -= timestep; if (++numUpdateSteps >= 240) { panic(); break; } } draw(delta / timestep); T.textContent = timestamp; if (timestamp >= 6000.0) { T.textContent = "Stopped!"; stop(); } end(fps); frameID = requestAnimationFrame(mainLoop);}//...The cancelAnimationFrame function is not stopping the animating loop. Got any suggestions? I have scratched my head over it for a long time now please any suggestions would be appreciated. 解决方案 When the condition for stop() is fulfilled, stop() is called but the code continues so a new rAF will be requested.Just add a return after the stop call to prevent this from happening (or use an else):...if (timestamp >= 6000.0) { T.textContent = "Stopped!"; stop(); // stop() is just a sub-routine here and will continue after being called return; // <--- here}...Modified fiddle 这篇关于为什么cancelAnimationFrame不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 08:28