2011-08-08 20 views

Trả lời

3

Tôi sẽ xử lý sự kiện phát và sau đó sử dụng theo dõi cuộc gọi setTimeout khi chúng hoàn tất. Nội dung như thế này (mã giả):

var timeHandler = null; 

// if they stop listening, don't give them the alert 
    myAudioElement.addEventListener('stop', function() { 
    if (timeHandler) clearTimeout(timeHandle); 
    }); 

// when they start to play, set an event to pop up 5 seconds later 
    myAudioElement.addEventListener('play', function() { 
     timeHandler = setTimeout(5000,function() { 
      // here I would make some kind of ajax call to log the event 
      alert("it's been 5 seconds!"); 
     }); 

Như mọi khi, điều này có thể phức tạp hơn tùy theo nhu cầu của bạn. Bạn cũng có thể sử dụng sự kiện ontimeupdate để theo dõi vị trí hiện tại của playhead. Để tham khảo về các sự kiện âm thanh html5, hãy xem trang này:

https://developer.mozilla.org/en/DOM/Media_events

Chúc may mắn!

+0

Có thể có sự chậm trễ sau sự kiện' 'phát'' và khi âm thanh thực sự bắt đầu phát. Sẽ an toàn hơn khi thiết lập trình xử lý sự kiện vào '' timeupdate''. – mzedeler

Các vấn đề liên quan