2011-05-02 93 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); 
}