カウントダウンするタイマーを作成しようとしていますが、ボタンをクリックすると停止します。私が変更したhereからコードをオンラインにしましたが、jQueryのポストメソッドを呼び出そうとすると不正な呼び出しエラーが発生します。私はそれが範囲の問題だとはっきりと確信していますが、私はその周りの最善の方法を確信していません。私の変更されたコードは以下の通りです。オブジェクト内からjQueryを呼び出す
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){
\t clearTimeout(this[id].to);
},
finalStop:function(id){
\t clearTimeout(this[id].to);
\t
\t $.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);
}
\t
}
表示されるエラーメッセージの詳細について教えてください。 – amphetamachine
'timer.finalStop( 'climbTimer')の代わりに' timer.finalStop(id); 'を実行する必要があります。また、非推奨の '.unbind()'ではなく '.off( 'click')'を使用する方が良いでしょう。 – trincot