2013-07-17 1 views
3

여기 내 상황이 있습니다. 실행 시간을 단축해야하기 때문에 setInterval은 현명한 선택이 아닙니다. 맞습니까? 매 시간마다 적어도 4ms의 비용이 듭니다.requestAnimationFrame에 의해 setInterval을 대체하는 방법

그래서 setInterval 함수를 requestAnimationFrame으로 변경할 수는 있지만 requestAnimationFrame이 어떻게 작동하는지 이해하지 못합니다. 예를

// some code here 
var interval = setInterval(doSomething, 10) 
var progress = 0 
function doSomething(){ 
    if (progress != 100){ 
     // do some thing here 
    }else{ 
     clearInterval(interval) 
    } 
} 

내가 requestAnimationFrame 방법을 적용 할 수 있습니다 들어

?

+0

난 당신이 "기능 실행 시간을 단축 할 필요"가 무슨 뜻인지 모르겠어요. 'requestAnimationFrame'은 * 애니메이션 *에서 유용합니다. 즉, 애니메이션 그리기 사이클을 실제 화면 다시 그리기 (느리게 진행)와 동기화하려고 할 때 유용합니다. – voithos

+0

@voithos 실제로 함수는 페이지에 무언가를 그리는 데 거의 1 분이 걸리고, 함수를 간격 필드로 설정하면 훨씬 많은 시간이 소요됩니다. 그냥 requestAnimationFrame 도움이 궁금해? – Kimmi

+0

아무 것도 움직이지 않는 경우가 아닙니다. – voithos

답변

-1

는 바이올린에 더 나은 보이는 ->just the code,no animation

모든 일을하여 setInterval을 사용 simplification.No 필요에 대한 코드 내부에 주석. 간격을 지우려면 cancelAnimationFrame을 사용하십시오.

// This makes sure that there is a method to request a callback to update the graphics for next frame 

var requestAnimationFrame = 
     window.requestAnimationFrame ||  // According to the standard 
     window.mozRequestAnimationFrame || // For mozilla 
     window.webkitRequestAnimationFrame || // For webkit 
     window.msRequestAnimationFrame ||  // For ie 
     function (f) { window.setTimeout(function() { f(Date.now()); }, 1000/60); }; // If everthing else fails 

var cancelAnimationFrame = 
     window.cancelAnimationFrame || 
     window.mozCancelAnimationFrame || 
     window.webkitCancelAnimationFrame || 
     window.msCancelAnimationFrame; 

// your code here 

var progress = 0; 

function doSomething() { 
    if (progress != 100) { 
     // do something here 
     var myAnimation = requestAnimationFrame(doSomething); 
    } else { 
     // don't use clearInterval(interval) instead when you know that animation is completed use cancelAnimationFrame() 
     cancelAnimationFrame(myAnimation); 
    }   
} 

일부 링크 가치가 읽기 ->

  1. CreativeJs---the best explanation any one could give,Every begineer must read
  2. CancelAnimationFrame
  3. link 3-->in context of your question
  4. I found this fiddle on google,quite the same that you want.

다른 것들을 당신이해야 알아 :

관련 문제