Javascript DOM onratechange Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("ratechange",
       myScript);

In this example, we use the addEventListener() method to attach a "ratechange" event to a video element.

The playbackRate property is used to change the playing speed of the video.

View in separate window

<!DOCTYPE html>
<html>
<body>
<video id="myVideo" width="100" height="100" autoplay controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video><br>

<p id="demo"></p>
<button onclick="setPlaySpeed()" type="button">Set video to be play in slow motion</button>

<script>
// Get the video element with id="myVideo"
var x = document.getElementById("myVideo");

// Set the current playback speed of the video to 0.3 (slow motion)
function setPlaySpeed() {//  ww w  .j a  va  2 s.co  m
  x.playbackRate = 0.3;
}
x.addEventListener("ratechange", myFunction);

function myFunction() {
  document.getElementById("demo").innerHTML = "The playing speed of the video was changed";
}
</script>

</body>
</html>
Bubbles: No
Cancelable: No
Event type: Event
Supported HTML tags: <audio> and <video>



PreviousNext

Related