How to use the cancel function from raf

Find comprehensive JavaScript raf.cancel code examples handpicked from public code repositorys.

raf.cancel is a function that cancels an animation frame request previously scheduled with the window.requestAnimationFrame() method.

377
378
379
380
381
382
383
384
385
386
  } else {
    window.removeEventListener('mousemove', this.onMouseMove)
  }

  window.removeEventListener('resize', this.onWindowResize)
  rqAnFr.cancel(this.raf)
}

calibrate(x, y) {
  this.calibrateX = x === undefined ? this.calibrateX : x
fork icon0
star icon2
watch icon0

786
787
788
789
790
791
792
793
794
795
796
}


Engine.prototype.stop = function() {
    this.running = false
    if (this._frame !== 0)
        raf.cancel(this._frame)
    this._frame = 0
    return this
}

fork icon0
star icon0
watch icon0

How does raf.cancel work?

raf.cancel works by taking a single argument, which is a number representing the ID of the animation frame request that you want to cancel. When you call window.requestAnimationFrame() to schedule an animation frame, it returns an ID that you can use later to cancel the animation frame. The ID is a numeric value that identifies the animation frame request. To cancel the animation frame, you simply pass the ID to the raf.cancel function. The function will then remove the animation frame request from the queue and prevent it from being executed. raf.cancel is useful when you need to stop an animation that is currently running, or if you need to cancel an animation frame request that is no longer needed. By canceling the animation frame, you can free up system resources and improve performance. Note that the animation frame request ID is only valid for the current window session. If you need to cancel an animation frame request after the window has been closed or reloaded, you will need to store the ID in a persistent storage, such as a database or local storage.

781
782
783
784
785
786
787
788
789
790
    } else {
      window.removeEventListener('mousemove', this.onMouseMove);
    }

    window.removeEventListener('resize', this.onWindowResize);
    rqAnFr.cancel(this.raf);
  }
}, {
  key: 'calibrate',
  value: function calibrate(x, y) {
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Define a function to be called on each animation frame
function update() {
  // Do some animation-related tasks
  // ...

  // Schedule the next animation frame
  requestId = window.requestAnimationFrame(update);
}

// Start the animation loop
let requestId = window.requestAnimationFrame(update);

// Later, cancel the animation frame request
window.cancelAnimationFrame(requestId);

In this example, we define a function called update that will be called on each animation frame. The function performs some animation-related tasks and then schedules the next animation frame using window.requestAnimationFrame(). We start the animation loop by calling window.requestAnimationFrame() with the update function as an argument. The method returns a request ID that we store in a variable called requestId. Later, if we want to cancel the animation frame request, we can simply call window.cancelAnimationFrame() with the requestId as an argument. This will remove the animation frame request from the queue and prevent it from being executed. Note that window.cancelAnimationFrame() and window.requestAnimationFrame() are usually used together to create smooth and performant animations. By canceling the animation frame requests when they are no longer needed, we can avoid unnecessary resource usage and improve the overall performance of our animations.