Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akinsho/c210f87bb08eb6a6f895e8ca5eb0b8fe to your computer and use it in GitHub Desktop.
Save akinsho/c210f87bb08eb6a6f895e8ca5eb0b8fe to your computer and use it in GitHub Desktop.

Revisions

  1. @jacob-beltran jacob-beltran revised this gist Mar 6, 2017. No changes.
  2. @jacob-beltran jacob-beltran revised this gist Mar 6, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion requestAnimationFrame.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ startLoop() {
    }

    loop() {
    /* perform loop work here */
    // perform loop work here
    this.theoreticalComponentAnimationFunction()

    // Set up next iteration of the loop
  3. @jacob-beltran jacob-beltran revised this gist Mar 6, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions requestAnimationFrame.js
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ startLoop() {

    loop() {
    /* perform loop work here */
    this.theoreticalComponentAnimationFunction()

    // Set up next iteration of the loop
    this.frameId = window.requestAnimationFrame( this.loop )
  4. @jacob-beltran jacob-beltran revised this gist Mar 6, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion requestAnimationFrame.js
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,9 @@ startLoop() {
    }

    loop() {
    // perform loop work here
    /* perform loop work here */

    // Set up next iteration of the loop
    this.frameId = window.requestAnimationFrame( this.loop )
    }

  5. @jacob-beltran jacob-beltran created this gist Mar 6, 2017.
    27 changes: 27 additions & 0 deletions requestAnimationFrame.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // How to ensure that our animation loop ends on component unount

    componentDidMount() {
    this.startLoop();
    }

    componentWillUnmount() {
    this.stopLoop();
    }

    startLoop() {
    if( !this._frameId ) {
    this._frameId = window.requestAnimationFrame( this.loop );
    }
    }

    loop() {
    // perform loop work here

    this.frameId = window.requestAnimationFrame( this.loop )
    }

    stopLoop() {
    window.cancelAnimationFrame( this._frameId );
    // Note: no need to worry if the loop has already been cancelled
    // cancelAnimationFrame() won't throw an error
    }