คุณสมบัติ duration ของ HTML DOM Video ใช้สำหรับคืนค่าตัวเลขที่สอดคล้องกับความยาวของวิดีโอ (หน่วยวินาที)
หมายเหตุ: สำหรับสตรีมแบบสด (live streams) จะคืนค่า 'Inf' ซึ่งเป็นอนันต์ เนื่องจากสตรีมสดไม่มีระยะเวลาที่กำหนดไว้ล่วงหน้า
ไวยากรณ์ (Syntax)
การคืนค่าสตริง
mediaObject.duration
ตัวอย่างการใช้งาน
ตัวอย่างต่อไปนี้แสดงการใช้งานคุณสมบัติ HTML DOM Video duration:
<!DOCTYPE html> <html> <head> <title>HTML DOM Video duration</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } input[type="button"] { border-radius: 10px; } </style> </head> <body> <form> <fieldset> <legend>HTML-DOM-Video-duration</legend> <video id="demo" width="320" controls><source src="https://www.tutorialspoint.com/html5/foo.mp4" type="video/mp4"></video><br> <input type="button" onclick="getTrackDetails()" value="How many seconds of content is this video?"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var demo = document.getElementById("demo"); function getTrackDetails() { divDisplay.textContent = 'Duration: '+demo.duration+' seconds'; } </script> </body> </html>
ผลลัพธ์
ก่อนคลิกปุ่ม 'How many seconds of content is this video?':

หลังคลิกปุ่ม 'How many seconds of content is this video?':
