Javascript 有没有办法修改正在运行的倒计时?
我目前正在为学校组织的游戏制作一个游戏,使用html、css、javascript和jQuery 在这个游戏中,我使用了倒计时(这很好),但我希望当我的角色被敌人击中时,它能减少2秒。当倒计时达到0时,玩家将输掉比赛 为此,我使用了真正完整的“jChavannes”'github(),但我找不到在倒计时运行时修改倒计时的方法,即使“jChavannes”使用了很多工具来使用他的工作 这是我想让它工作的HTML代码,带有我需要修改倒计时的按钮,以及我用来让整个事情工作的javascript(缺少一个库,但它太大了,放不到这里,你可以在Github中找到它) 是否有一种简单的方法来修改剩余时间,或者它只是设计为在不停止和重置它的情况下进行修改Javascript 有没有办法修改正在运行的倒计时?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我目前正在为学校组织的游戏制作一个游戏,使用html、css、javascript和jQuery 在这个游戏中,我使用了倒计时(这很好),但我希望当我的角色被敌人击中时,它能减少2秒。当倒计时达到0时,玩家将输掉比赛 为此,我使用了真正完整的“jChavannes”'github(),但我找不到在倒计时运行时修改倒计时的方法,即使“jChavannes”使用了很多工具来使用他的工作 这是我想让它工作的HTML代码,带有我需要修改倒计时的按钮,以及我用来让整个事情工作的javascript(缺少一
var Example2=new(函数(){
var$倒计时,
$form,//用于更改倒计时时间的表单
递增时间=70,
当前时间=30000,
updateTimer=函数(){
$countdown.html(formatTime(currentTime));
如果(currentTime==0){
示例2.Timer.stop();
timerComplete();
示例2.重置倒计时();
返回;
}
currentTime-=递增时间/10;
如果(currentTime<0)currentTime=0;
},
timerComplete=函数(){
警报('示例2:倒计时计时器完成!');
},
init=函数(){
$倒计时=$(“#倒计时”);
示例2.Timer=$.Timer(updateTimer,incrementTime,true);
$form=$('example2form');
$form.bind('submit',function(){
示例2.重置倒计时();
返回false;
});
};
this.resetCountdown=函数(){
var newTime=parseInt($form.find('input[type=text]')).val())*100;
如果(newTime>0){currentTime=newTime;}
这个.Timer.stop().one();
};
美元(初始);
});
//我试图触发一些命令,但没有成功
$(“#timerOnce5000”)。单击(函数(){
console.log(“是吗?”);
$(“#倒计时”)。计时器。一次(5000);
});
$(“#timerSetTime1000”)。单击(函数(){
console.log(“全部”?);
$(“#倒计时”).timer.set({time:1000});
});
$(“#timerSetTime5000”)。单击(函数(){
控制台日志(“你好?”);
$(“#倒计时”).timer.set({time:5000});
});代码>
jQuery定时器演示
示例2-倒计时计时器
05:00:00
您是否在模拟“被敌人击中”,在这种情况下,请尝试:
var Example2 = new (function() {
...
...
this.hitByEnemy() = function {
currentTime = currentTime - 2000; //assuming milliseconds is units
});
});
HTML:
...
Javascript最酷的一点(即使有时有点危险)是,修改共享变量/资源以影响代码其他部分的行为通常非常容易。有些人不喜欢这样,因为如果你把你的函数交织在一起,它会使调试变得困难。我个人认为它在像您这样的有限场景中非常有用
因此,我的方法就是在玩家被击中时使用一个函数来修改当前时间。比如:
var $countdown,
$form,
// ... the other stuff you had in your provided code
hitHandler = function() { // you'll need to call this when your player gets hit
currentTime -= 2 * incrementTime;
if (currentTime < 0) currentTime = 0;
}
var$倒计时,
$form,
// ... 您提供的代码中的其他内容
hitHandler=function(){//当玩家被击中时,需要调用此函数
currentTime-=2*递增时间;
如果(currentTime<0)currentTime=0;
}
也就是说,您还可以添加一个新的变量来记录点击次数,并在定期更新计时器时使用该变量。这将是“更干净”的解决方案,但它也有点冗长,如果不想在递减计时器之前等待递增函数,它可能不是您想要的。也就是说,它看起来像:
var $countdown,
$form,
// ...
hits = 0
hitHandler = function() {
hits++;
}
updateTimer = function() {
$countdown.html(formatTime(currentTime));
if (currentTime == 0) {
Example2.Timer.stop();
timerComplete();
Example2.resetCountdown();
return;
}
currentTime -= incrementTime / 10;
currentTime -= hits * (2 * incrementTime);
hits = 0;
if (currentTime < 0) currentTime = 0;
}
var$倒计时,
$form,
// ...
点击率=0
hitHandler=函数(){
hits++;
}
updateTimer=函数(){
$countdown.html(formatTime(currentTime));
如果(currentTime==0){
示例2.Timer.stop();
timerComplete();
示例2.重置倒计时();
返回;
}
currentTime-=递增时间/10;
currentTime-=点击次数*(2*递增时间);
点击率=0;
如果(currentTime<0)currentTime=0;
}
将currentTime
移动到this
上,而不是var
,或者提供一些方法来修改currentTime
我没有想到在Example2函数中使用这个。我尝试了一下,它很有效!非常感谢你!!
var $countdown,
$form,
// ...
hits = 0
hitHandler = function() {
hits++;
}
updateTimer = function() {
$countdown.html(formatTime(currentTime));
if (currentTime == 0) {
Example2.Timer.stop();
timerComplete();
Example2.resetCountdown();
return;
}
currentTime -= incrementTime / 10;
currentTime -= hits * (2 * incrementTime);
hits = 0;
if (currentTime < 0) currentTime = 0;
}