【发布时间】:2017-03-24 19:09:06
【问题描述】:
我正在尝试创建一个计时器来计数,然后在单击按钮时停止。我从here 在线获得了一些我修改过的代码,但是当我尝试调用 jQuery post 方法时出现非法调用错误。我很确定这是一个范围问题,但我不确定解决它的最佳方法。我修改后的代码如下。
var timer={
init:function(id){
this[id]={
obj:document.getElementById(id)
}
},
start:function(id){
var obj=this[id];
obj.srt=new Date();
clearTimeout(obj.to);
this.tick(id);
$( "#climbTimer" ).unbind();
$('#climbTimer').click(function(){timer.finalStop('climbTimer'); return false;});
if ($('#'+id).html().indexOf('Start') != -1)
$('#'+id).html('Stop Ascend<br><span id="elapsedTime">0.00</span>');
},
stop:function(id){
clearTimeout(this[id].to);
},
finalStop:function(id){
clearTimeout(this[id].to);
$.post('php/climbTime.php', {team: document.getElementById('team').value, roundNum: document.getElementById('round').value, time: document.getElementById('elapsedTime')});
},
tick:function(id){
this.stop(id);
var obj=this[id],sec=(new Date()-obj.srt)/1000,min=Math.floor(sec/60),sec=sec%60;
$('#elapsedTime').html(sec>9?sec:''+sec);
obj.to=setTimeout(function(){ timer.tick(id); },100);
}
}
【问题讨论】:
-
您能否更具体地了解您收到的错误消息?
-
您需要使用
timer.finalStop(id);而不是timer.finalStop('climbTimer')。最好不要使用已弃用的.unbind(),而是使用.off('click')。
标签: javascript jquery scope