大佬教程收集整理的这篇文章主要介绍了javascript – 有没有办法修改正在运行的倒计时?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个由我的学校组织的游戏果酱游戏,使用html,css,javascript和jQuery.
在这个游戏中,我正在使用倒计时(效果很好),但是当我的角色被敌人击中时,我希望它会失去2秒.当倒计时达到0时,玩家将失去游戏.
为了做到这一点,我正在使用“jChavannes”’github(Link here)这是非常完整的,但我找不到一种方法来修改倒计时,尽管’jchavannes’提供了很多工具来使用他的工作.
这是我想要做的工作的HTML代码,带有我需要修改倒计时的按钮和我用来使整个事情工作的javascript(一个库丢失,但它太大了)放在这里,你会在Github找到它.
是否有简单的方法来修改剩余时间,或者它是否只是在没有停止和重置的情况下被设计为不被修改?
var Example2 = new (function() {
var $countdown,
$form, // Form used to change the countdown time
incrementTime = 70,
currentTime = 30000,
updatetiR_240_11845@er = function() {
$countdown.html(formatTime(currentTimE));
if (currentTime == 0) {
Example2.Timer.stop();
timerComplete();
Example2.resetCountdown();
return;
}
currentTime -= incrementTime / 10;
if (currentTime < 0) currentTime = 0;
},
timerComplete = function() {
alert('Example 2: Countdown timer complete!');
},
init = function() {
$countdown = $('#countdown');
Example2.Timer = $.timer(updatetiR_240_11845@er, incrementTime, truE);
$form = $('#example2form');
$form.bind('submit', function() {
Example2.resetCountdown();
return false;
});
};
this.resetCountdown = function() {
var newTime = parseInt($form.find('input[type=text]').val()) * 100;
if (newTime > 0) {CurrentTime = newTime;}
this.Timer.stop().once();
};
$(init);
});
//I tried to trigger some commands, in vain
$("#timerOnce5000").click(function(){
console.log("euh ouais ?");
$("#countdown").timer.once(5000);
});
$("#timerSetTime1000").click(function(){
console.log("allô ?");
$("#countdown").timer.set({time:1000});
});
$("#timerSetTime5000").click(function(){
console.log("bonjour ?");
$("#countdown").timer.set({time:5000});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<Meta name="author" content="Jason Chavannes" />
<title>jQuery Timer Demo</title>
<link rel="stylesheet" href="res/style.css" />
<script type="text/javascript" src="res/jquery.min.js"></script>
<script type="text/javascript" src="jquery.timer.js"></script>
<script type="text/javascript" src="res/demo.js"></script>
</head>
<body>
<h3>Example 2 - Countdown Timer</h3>
<span id="countdown">05:00:00</span>
<form id="example2form">
<input type='button' value='Play/Pause' onclick='Example2.Timer.toggle();' />
<input type='button' value='Stop/Reset' onclick='Example2.resetCountdown();' />
<input type='text' name='startTime' value='300' style='width:30px;' />
</form>
<div class='example-four'>
<input type='button' value='timer.reset()' onclick='timer.reset();' /><br/>
<input type='button' value='timer.once()' onclick='timer.once();' /><br/>
<input type='button' value='timer.once(5000)' id="timerOnce5000" onclick='timer.once(5000);' /><br/>
<input type='button' value='timer.set({time:1000})' id ="timerSetTime1000" onclick='timer.set({time:1000});' /><br/>
<input type='button' value='timer.set({time:5000})' id="timerSetTime5000" onclick='timer.set({time:5000});' />
</div>
<br/>
解决方法:
你在模拟’被敌人击中’,在这种情况下尝试:
var Example2 = new (function() {
...
...
this.hitByEnemy() = function {
currentTime = currentTime - 2000; //assuming milliseconds is units
});
});
HTML:
<div class='example-four'>
...
<input type='button' value='hit by enemy' onclick='Example2.hitByEnemy();' /><br/>
</div>
以上是大佬教程为你收集整理的javascript – 有没有办法修改正在运行的倒计时?全部内容,希望文章能够帮你解决javascript – 有没有办法修改正在运行的倒计时?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。