2011-05-02 58 views
0

我遇到了這個JavaScript腳本的問題。我已經嘗試了許多事情來實現它。目前那裏的警報出於調試的目的,似乎沒有發生。調試JavaScript計時事件

請幫忙嗎?

function checkTime(this_time){ 
var the_string = "checkTime("+this_time+")"; 
var now = ((new Date()).getTime()); 
if(parseInt(now) >= parseInt(this_time)){ 
    document.write("TIMEUP!"); 
} 
alert(now); 
alert(this_time); 
var t = setTimeout(the_string,300); 
} 

var the_time = (((new Date()).getTime())+19000); 
var the_string = "checkTime("+the_time+")"; 
var t = setTimeout(the_string,300); 

感謝,

意志。

回答

1

好像你正在尋找一個倒計時?
this fiddle。代碼被簡化爲:

var bench = 19000 + new Date().getTime(), 
    timer = setInterval(
       function(){ 
       checkTime(bench); 
       } 
       , 1000 
      ); 

function checkTime(this_time){ 
    var check = new Date - this_time; 
    if(check>=0){ 
     alert('time\'s up!'); 
     clearInterval(timer); 
    } 
} 
+0

看到我的小提琴沒有外部變種:) – mplungjan 2011-05-02 13:11:11

+0

@mplungjan:葉,看到它。另見http://jsfiddle.net/KooiInc/Bye2V/ – KooiInc 2011-05-02 13:36:39

1

你應該使用setTimeout而不是字符串的閉包。

var now = new Date().getTime(); 
setTimeout(function(){ 
    //your Javascript code here 
    //"now" can be used here as a closure 
}, 300); 
+0

@Mic感謝您的迴應。我試圖改變腳本,但它仍然無法正常工作。沒有一個警報框。 :/ – 2011-05-02 12:33:09

+0

這是一個很好的第一步;)當你玩定時事件時,'alert'停止流程,你應該使用'console.log(...)'但不幸的是我不明白你想要做什麼與此代碼。 – Mic 2011-05-02 12:38:02

+0

如果你想在19秒後作出反應,爲什麼不在超時時間內放置19000?如果你想檢查每300ms直到它達到19秒,你應該使用'setInterval'而不是'setTimeout' – Mic 2011-05-02 12:40:16

0

我想代碼可以做得更簡單。

function checkTime(this_time){ 
    var now = ((new Date()).getTime()); 
    if((now - this_time) >= 0){ 
     document.write("TIMEUP!"); 
     window.clearInterval(timer); 
    } 
} 

var t_t = (((new Date()).getTime())+19000); 

var timer = window.setInterval(function(){ 
    checkTime(t_t); } 
, 300); 

乾杯!

+0

@Ashshoot Soota非常感謝,但我插入alert();'進來測試存在this_time和現在但沒有一個單一的警報,寫入的字符串。 :/ – 2011-05-02 12:39:51

+0

'setTimeout(function(){document.write(「TIMEUP!」);},19000)'相同,不是嗎? :) – Mic 2011-05-02 12:42:34

+0

但我認爲麻煩的是,瀏覽器有時會計數19秒,因爲23秒的實際秒數,因爲JavaScript速度減慢? – 2011-05-02 12:47:05

1

這是一個更安全和獨立的版本。加載後到document.write將清除頁面完全

http://jsfiddle.net/mplungjan/Zt5k7/

window.onload=function() { 
    var timer = function (endTime) { 
    var end = new Date(endTime); 
    var tId; 
    this.checkTime=function(){ 
     var now = new Date(); 
     document.getElementById("msg").innerHTML=now.toLocaleString(); 
     if (now.getTime()>=end.getTime()) { 
     document.getElementById("msg").innerHTML="TIME's UP!"; 
     clearInterval(tId); 
     } 
    } 
    tId = setInterval(this.checkTime,300); 
    }(new Date().getTime()+5000); 
} 

或一個適當的倒計時http://jsfiddle.net/mplungjan/Zt5k7/1/

window.onload=function() { 
    var timer = function (endTime) { 
    var end = new Date(endTime); 
    var tId; 
    this.checkTime=function(){ 
     var now = new Date(); 
     document.getElementById("msg").innerHTML=now.toLocaleString(); 
     var diff = end.getTime()-now.getTime() 
     if (diff >= 1) document.getElementById("msg").innerHTML=parseInt(diff/1000)+1; 
     else { 
     document.getElementById("msg").innerHTML="TIME's UP!"; 
     clearInterval(tId); 
     } 
    } 
    tId = setInterval(this.checkTime,300); 
    }(new Date().getTime()+9000); 
}