เหตุการณ์การอัพเดทเวลา DOM เสียง/วิดีโอ HTML
❮ การอ้างอิง DOM เสียง/วิดีโอ HTML
ตัวอย่าง
เมื่อตำแหน่งการเล่นของวิดีโอเปลี่ยนไป ให้แสดงตำแหน่งปัจจุบันของวิดีโอเป็นวินาที:
// Get the <video> element with id="myVideo"
var vid = document.getElementById("myVideo");
// Assign an ontimeupdate event to the <video> element, and execute a function if the current playback position has changed
vid.ontimeupdate = function() {myFunction()};
function myFunction() {
// Display the current position of the video in a <p> element with id="demo"
document.getElementById("demo").innerHTML = vid.currentTime;
}
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
เหตุการณ์ timeupdate เกิดขึ้นเมื่อตำแหน่งการเล่นของเสียง/วิดีโอเปลี่ยนไป
เหตุการณ์นี้ถูกเรียกใช้โดย:
- การเล่นเสียง/วิดีโอ
- การย้ายตำแหน่งการเล่น (เช่น เมื่อผู้ใช้กรอไปข้างหน้าอย่างรวดเร็วไปยังจุดอื่นในเสียง/วิดีโอ)
เคล็ดลับ:เหตุการณ์ timeupdate นี้มักใช้ร่วมกับ คุณสมบัติ currentTimeของ Audio/Video Object ซึ่งจะคืนค่าตำแหน่งปัจจุบันของการเล่นเสียง/วิดีโอในหน่วยวินาที
รองรับเบราว์เซอร์
ตัวเลขในตารางระบุเบราว์เซอร์รุ่นแรกที่รองรับเหตุการณ์อย่างสมบูรณ์
Event | |||||
---|---|---|---|---|---|
timeupdate | Yes | 9.0 | Yes | Yes | Yes |
ไวยากรณ์
ใน HTML:
<audio|video ontimeupdate="myScript">
ในจาวาสคริปต์:
audio|video.ontimeupdate=function(){myScript};
ใน JavaScript โดยใช้เมธอด addEventListener() :
audio|video.addEventListener("timeupdate", myScript);
รายละเอียดทางเทคนิค
แท็ก HTML ที่รองรับ: | <audio> และ <video> |
---|---|
วัตถุ JavaScript ที่รองรับ: | วิดีโอเสียง |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
เมื่อตำแหน่งการเล่นของเสียงเปลี่ยนไป ให้แสดงตำแหน่งปัจจุบันของเสียงเป็นวินาที:
// Get the <audio> element with id="myVideo"
var aud = document.getElementById("myVideo");
// Assign an ontimeupdate event to the <audio> element, and execute a function if the current playback position has changed
aud.ontimeupdate = function() {myFunction()};
function myFunction() {
// Display the current position of the audio in a <p> element with id="demo"
document.getElementById("demo").innerHTML = aud.currentTime;
}
ตัวอย่าง
ใช้คุณสมบัติ currentTime เพื่อตั้งค่าตำแหน่งการเล่นปัจจุบันเป็น 5 วินาที:
document.getElementById("myVideo").currentTime = 5;
❮ การอ้างอิง DOM เสียง/วิดีโอ HTML