Javascript 是否将现有的ClickEvent添加到按钮?
我有一个用javascript编写的计时器。当我在HTML页面中单击计时器时,它将暂停。如果我再次单击它,它将继续计数 这是计时器代码:Javascript 是否将现有的ClickEvent添加到按钮?,javascript,jquery,button,timer,mouseclick-event,Javascript,Jquery,Button,Timer,Mouseclick Event,我有一个用javascript编写的计时器。当我在HTML页面中单击计时器时,它将暂停。如果我再次单击它,它将继续计数 这是计时器代码: var Timer = function() {} Timer.prototype = { refresh: null, focus: null, sec: 0, min: 0, hour: 0, paused: false, init: function(el) { var that =
var Timer = function() {}
Timer.prototype = {
refresh: null,
focus: null,
sec: 0,
min: 0,
hour: 0,
paused: false,
init: function(el) {
var that = this;
this.el = el;
this.clickEvent();
return this;
},
set: function(t) {
t = t.split(':');
this.hour = t[0];
this.min = t[1];
this.sec = t[2];
return this;
},
bindFocus: function() {
var that = this;
clearInterval(this.focus);
if (document.hasFocus)
that.focus = setInterval(function() {
if (document.hasFocus()) {
if (that.paused) {
window.clearInterval(this.refresh);
that.go();
}
} else
that.stop();
}, 200);
},
clickEvent: function() {
var that = this, frag = $('<h2>').addClass('pauseGame').text(texts.pause), toggleFlag = true;
this.el.bind('click', timerClicked);
function timerClicked(callback) {
if (!toggleFlag)
return;
toggleFlag = false;
if (that.paused === false) {
that.stop();
window.clearInterval(that.focus);
$('#options > button').not('#options > .pause').prop('disabled', true);
$('#options > .pause').text('Resume');
board.mainBoard.fadeOut(400, function() {
frag.css({
letterSpacing: '25px',
opacity: 0
});
$(this).after(frag).detach();
frag.parent().addClass('paused');
frag.animate({
opacity: 1
}, {
queue: false,
duration: 400
}).animate({
letterSpacing: '-4px'
}, 700, "easeOutBack", function() {
toggleFlag = true;
});
});
that.el.addClass('pause');
} else {
$('#options > button').prop('disabled', false);
$('#options > .pause').text('Pause');
options.undoToggle();
frag.animate({
opacity: 0,
letterSpacing: '25px'
}, 600, "easeInBack", function() {
$(this).parent().removeClass('paused').end().remove();
board.container.prepend(board.mainBoard).removeAttr('style');
board.mainBoard.fadeIn(400);
that.go();
toggleFlag = true;
});
this.className = '';
}
}
},
restart: function(el) {
this.sec = this.min = this.hour = 0;
this.el.text('00:00');
this.stop().go();
},
go: function(now) {
var that = this;
this.paused = false;
if (now)
updateClock();
window.clearInterval(this.refresh);
that.refresh = window.setInterval(updateClock, 1000);
function updateClock() {
that.sec++;
if (that.sec == 60) {
that.sec = 0;
that.min++;
}
if (that.sec < 10)
that.sec = "0" + that.sec;
if (that.min == 60) {
that.min = 0;
that.hour++;
}
var hours = (that.hour > 0) ? ((that.hour <= 9) ? "0" + that.hour : that.hour) + ":": '';
that.el.html(hours + ((that.min <= 9) ? "0" + that.min : that.min) + ":" + that.sec);
}
return this;
},
stop: function() {
this.paused = true;
window.clearInterval(this.refresh);
return this;
}
};
var Timer=function(){}
Timer.prototype={
刷新:空,
焦点:空,
第0节,
分:0,,
小时:0,
暂停:错,
初始化:函数(el){
var=这个;
this.el=el;
单击此项。单击事件();
归还这个;
},
设置:功能(t){
t=t.split(“:”);
该时间=t[0];
this.min=t[1];
this.sec=t[2];
归还这个;
},
bindFocus:function(){
var=这个;
clearInterval(这是焦点);
if(document.hasFocus)
that.focus=setInterval(函数(){
if(document.hasFocus()){
如果(那.暂停){
window.clearInterval(this.refresh);
那就去吧;
}
}否则
那。停止();
}, 200);
},
clickEvent:函数(){
var that=this,frag=$('').addClass('pauseGame').text(text.pause),toggleFlag=true;
此.el.bind('click',timerClicked);
函数timerClicked(回调){
如果(!toggleFlag)
返回;
toggleFlag=false;
如果(that.paused==false){
那。停止();
window.clearInterval(即.focus);
$(“#选项>按钮”).not(“#选项>暂停”).prop('disabled',true);
$('#选项>.pause').text('Resume');
主板衰减(400,功能(){
frag.css({
字母间距:“25px”,
不透明度:0
});
$(this).after(frag.detach();
frag.parent().addClass('paused');
frag.animate({
不透明度:1
}, {
队列:false,
持续时间:400
}).制作动画({
字母间距:'-4px'
},700,“EaseAutback”,函数(){
toggleFlag=true;
});
});
即.el.addClass('pause');
}否则{
$(“#选项>按钮”).prop('disabled',false);
$('#选项>.pause')。文本('pause');
选项。撤消切换();
frag.animate({
不透明度:0,
字母间距:“25px”
},600,“easeInBack”,函数(){
$(this).parent().removeClass('paused').end().remove();
board.container.prepend(board.mainBoard).removeAttr('style');
主板,fadeIn(400);
那就去吧;
toggleFlag=true;
});
this.className='';
}
}
},
重新启动:功能(el){
this.sec=this.min=this.hour=0;
此.el.文本('00:00');
这个。停止()。走();
},
go:功能(现在){
var=这个;
this.pause=false;
如果(现在)
updatelock();
window.clearInterval(this.refresh);
that.refresh=window.setInterval(updatelock,1000);
函数updatelock(){
那是.sec++;
如果(该秒=60){
即秒=0;
那.min++;
}
如果(该秒<10)
that.sec=“0”+that.sec;
如果(that.min==60){
即:0.min=0;
那.小时++;
}
var hours=(that.hour>0)?((that.hour要模拟点击吗?
明白了:
timer.el.trigger('click')
我有这个窍门吗你能给我们一点关于你在代码示例中做了哪些更改的上下文吗?现在,这个答案并没有真正告诉我们如何解决这个问题,以及为什么上面的答案会解决这个问题。当然,尽管这是一个线索和错误。计时器代码中的这一部分触发/绑定了暂停:“this.el.bind”('click',timerClicked);“所以我用这个来模拟它。当我现在点击按钮时,它“模拟”了对计时器的点击。亚历山大的评论为我指明了正确的方向。
<div id="options">
<button class="pause" title="pause/resume the game">Pause</button>
</div>