javascript为chrome发送自定义事件 [英] javascript firing custom event for chrome
问题描述
你能给我一个chrome的例子我有这样的东西:
can you give me an example for chrome where i have something like this:
function checkVideoStatus(vidElement){
var currentTime = vidElement.currentTime;
//now wait 5 seconds
var updatedTime = vidElement.currentTime;
//now check if video has freezed
if(updatedTime == currentTime){
//fire videoFreeze Event
}
}
所以我想在这里解雇这个事件。此外,是否真的有必要触发这个事件,因为我可以在这里调用相关的方法来做我想要的事情吗?
So i would want to fire this event here. Also, is it really necessary to fire this event, since i alredy can put a relevant method call here to do what i want accordingly??
它是否在JS事件中只是像onChange,onMouseOver等?
Is it in JS the events are only like onChange, onMouseOver etc??
推荐答案
这是一个自定义事件和倒计时的简单模型。
This is a simple mockup of a custom event and a countdown.
<div id="vid" data-current="0">Video</div>
var vid = document.getElementById('vid'),
freeze;
vid.addEventListener('freeze', function() {
console.log('Firing vid.freeze.... ' + this.dataset.current);
console.log('vid.freeze fired.');
});
(function go() {
var status;
vid.innerHTML = 'Video time: ' + vid.dataset.current;
if (vid.dataset.current < 5) {
status = vid.dataset.current == 0 ? 'Starting... ' : 'Continuing... ';
console.log(status + vid.dataset.current);
setTimeout(go, 1000);
} else {
freeze = document.createEvent('CustomEvent');
freeze.initEvent('freeze', true, true);
vid.dispatchEvent(freeze);
}
vid.dataset.current++;
})();
http://jsfiddle.net/userdude/Ampbm/1
这主要是为了展示如何制作一个在五秒钟后触发的自定义事件。我还使用 data -
属性来存储当前时间。您在中执行的操作
块我不太确定,因为 .updatedTime
和 .currentTime
在我看来可能是相同的事情。
This is mostly to show how to make a custom event which fires after five seconds. I'm also using the data-
attribute to store the current time. What you're doing in the if
block I'm not quite sure about, since .updatedTime
and .currentTime
seems to me to be possibly the same thing each go round.
这篇关于javascript为chrome发送自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!