- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
haha = 0;
function dodat(){
haha++;
code here...
}
setInterval(function(){
dodat();
}, 1500-haha);
很奇怪,时间似乎并没有真正添加到 setinterval 中......
这是我的代码,SetInterval 从第 57
行开始,函数 dodat()
:
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<!DOCTYPE html>
<body onkeypress="pretend();">
<span id="money">25</span>$ - <span id="lives">100</span>/100 lives
<br><br>
<span style="background-color:#2cafe4;cursor:pointer;width:1000px;height:175px;overflow:hidden;position:relative;display:block;" id="track"></span>
<br>
<span id="divthing" style="position:relative;display:block;"></span>
<script>
money = 25;
lives = 100;
mycars = {};
original = 0;
function doofus() {
if($("div:first").offset().left > 1000){
$("div:first").remove();
lives = lives-1;
document.getElementById("lives").innerHTML = lives;
}
}
haha = 0;
function dodat() {
haha = haha+100;
var btn = document.createElement("div");
anyArray = new Array('http://i44.tinypic.com/i4l1r8.png', 'http://i41.tinypic.com/nxs4k8.jpg');
function randOrd(){
return (Math.round(Math.random())-0.5); }
anyArray.sort(randOrd);
btn.innerHTML="<img src='"+anyArray+"' style='height:60px;'>";
btn.style.position = "absolute";
btn.style.left = "0px";
var numba = Math.round(Math.random() * 50);
btn.class = "haha";
btn.id = numba;
mycars[numba] = -50;
var move = function () {
mycars[numba] = mycars[numba] + 1.5;
document.getElementById(numba).style.left = mycars[numba] + "px";
if(mycars[numba] > 100 && mycars[numba] < 150){
document.getElementById(numba).style.top = mycars[numba]/0.5-200 + "px";
}
};
setInterval(move, 10);
document.getElementById("track").appendChild(btn);
}
setInterval(function(){
dodat();
}, 1500-haha);
setInterval(doofus, 200);
function dis1() {
$("#shoot1").css("background-color", "red");
setTimeout('$("#shoot1").css("background-color", "blue");', '1000');
compareEl = $("#shoot1");
// Let's find the closest block!
var otherEls = $('div'),
compareTop = compareEl.offset().top,
compareLeft = compareEl.offset().left,
winningScore = Infinity,
score, winner, curEl;
otherEls.each(function () {
// Calculate the score of this element
curEl = $(this);
score = Math.abs(curEl.offset().left - compareLeft);
if (score < winningScore) {
winningScore = score;
winner = this;
}
});
$("#"+winner.id+"").remove();
money = money+1;
document.getElementById("money").innerHTML=""+money+"";
}
function dis2() {
$("#shoot2").css("background-color", "red");
setTimeout('$("#shoot2").css("background-color", "blue");', '1000');
compareEl2 = $("#shoot2");
// Let's find the closest block!
var otherEls2 = $('div'),
compareTop2 = compareEl2.offset().top,
compareLeft2 = compareEl2.offset().left,
winningScore2 = Infinity,
score2, winner2, curEl2;
otherEls2.each(function () {
// Calculate the score of this element
curEl2 = $(this);
score2 = Math.abs(curEl2.offset().left - compareLeft2);
if (score2 < winningScore2) {
winningScore2 = score2;
winner2 = this;
}
});
$("#"+winner2.id+"").remove();
money = money+1;
document.getElementById("money").innerHTML=""+money+"";
}
function dis3() {
$("#shoot3").css("background-color", "red");
setTimeout('$("#shoot3").css("background-color", "blue");', '1000');
compareEl3 = $("#shoot3");
// Let's find the closest block!
var otherEls3 = $('div'),
compareTop3 = compareEl3.offset().top,
compareLeft3 = compareEl3.offset().left,
winningScore3 = Infinity,
score3, winner3, curEl3;
otherEls3.each(function () {
// Calculate the score of this element
curEl3 = $(this);
score3 = Math.abs(curEl3.offset().left - compareLeft3);
if (score3 < winningScore3) {
winningScore3 = score3;
winner3 = this;
}
});
$("#"+winner3.id+"").remove();
money = money+1;
document.getElementById("money").innerHTML=""+money+"";
}
function dis4() {
$("#shoot4").css("background-color", "red");
setTimeout('$("#shoot4").css("background-color", "blue");', '1000');
compareEl4 = $("#shoot4");
// Let's find the closest block!
var otherEls4 = $('div'),
compareTop4 = compareEl4.offset().top,
compareLeft4 = compareEl4.offset().left,
winningScore4 = Infinity,
score4, winner4, curEl4;
otherEls4.each(function () {
// Calculate the score of this element
curEl4 = $(this);
score4 = Math.abs(curEl4.offset().left - compareLeft4);
if (score4 < winningScore4) {
winningScore4 = score4;
winner4 = this;
}
});
$("#"+winner4.id+"").remove();
money = money+1;
document.getElementById("money").innerHTML=""+money+"";
}
function dis5() {
$("#shoot5").css("background-color", "red");
setTimeout('$("#shoot4").css("background-color", "blue");', '1000');
compareEl5 = $("#shoot5");
// Let's find the closest block!
var otherEls5 = $('div'),
compareTop5 = compareEl5.offset().top,
compareLeft5 = compareEl5.offset().left,
winningScore5 = Infinity,
score5, winner5, curEl5;
otherEls5.each(function () {
// Calculate the score of this element
curEl5 = $(this);
score5 = Math.abs(curEl5.offset().left - compareLeft5);
if (score5 < winningScore5) {
winningScore5 = score5;
winner5 = this;
}
});
$("#"+winner5.id+"").remove();
money = money+1;
document.getElementById("money").innerHTML=""+money+"";
}
function whatareyousingingpatrick(){
if(money >= 15){
money = money-15;
original = original+1;
setInterval("dis"+original+"();", 2400);
var btn = document.createElement("shooter");
btn.style.display = "block";
btn.id = "shoot"+original+"";
btn.style.height = "25px";
btn.style.width = "25px";
btn.style.backgroundColor = "blue";
btn.innerHTML = "<img src='http://www.bubblews.com/assets/images/news/1317280976_1370202845.png' style='height:100%;width:100%;border-radius:100%;opacity:0.7;'>";
btn.style.borderRadius= "100%";
btn.style.boxShadow= "0px 0px 200px 75px rgba(0, 0, 0, 0.2)";
btn.style.position = "absolute";
btn.style.left = event.pageX-8;
btn.style.top = event.pageY-44;
document.getElementById("track").appendChild(btn);
}
else{
alert("Sorry, this dude costs 15 bucks.");
}
}
function whatareyousingingpatrick2(){
if(money >= 25){
money = money-25;
original = original+1;
setInterval("dis"+original+"();", 2000);
var btn = document.createElement("shooter");
btn.style.display = "block";
btn.id = "shoot"+original+"";
btn.style.height = "25px";
btn.style.width = "25px";
btn.style.backgroundColor = "blue";
btn.innerHTML = "<img src='http://static3.wikia.nocookie.net/__cb62439/xwing-miniatures/images/thumb/1/18/Missile_Icon.png/100px-0,111,0,110-Missile_Icon.png' style='height:100%;width:100%;border-radius:100%;opacity:0.7;'>";
btn.style.borderRadius= "100%";
btn.style.boxShadow= "0px 0px 200px 75px rgba(0, 0, 0, 0.2)";
btn.style.position = "absolute";
btn.style.left = event.pageX-8;
btn.style.top = event.pageY-44;
document.getElementById("track").appendChild(btn);
}
else{
alert("Sorry, this dude costs 25 bucks.");
}
}
function pretend(){
if (event.keyCode == 13) {
if(money >= 5){
money = money-5;
$("div").hide();
alert("You have bought the INSTANT KILL feature. Note that you can purchase this feature an unlimited number of times.");
}
else
{
alert("Sorry, the cost of the INSTANT KILL feature is 5$");
}
}
if (event.keyCode == 49) {
if(money >= 40){
money = money-40;
alert("You have bought the FASTER SHOOTING upgrade for your first missile. Note that you can purchase this upgrade an unlimited number of times.");
setInterval("dis1();", "8000");
}
else
{
alert("Sorry, the cost of the FASTER SHOOTING upgrade for this missile is 40$");
}
}
if (event.keyCode == 50) {
if(money >= 40){
money = money-40;
alert("You have bought the FASTER SHOOTING upgrade for your second missile. Note that you can purchase this upgrade an unlimited number of times.");
setInterval("dis2();", "8000");
}
else
{
alert("Sorry, the cost of the FASTER SHOOTING upgrade for this missile is 40$");
}
}
if (event.keyCode == 51) {
if(money >= 40){
money = money-40;
alert("You have bought the FASTER SHOOTING upgrade for your third missile. Note that you can purchase this upgrade an unlimited number of times.");
setInterval("dis3();", "8000");
}
else
{
alert("Sorry, the cost of the FASTER SHOOTING upgrade for this missile is 40$");
}
}
if (event.keyCode == 52) {
if(money >= 40){
money = money-40;
alert("You have bought the FASTER SHOOTING upgrade for your fourth missile. Note that you can purchase this upgrade an unlimited number of times.");
setInterval("dis4();", "8000");
}
else
{
alert("Sorry, the cost of the FASTER SHOOTING upgrade for this missile is 40$");
}
}
}
</script>
<br><br>
<button onclick='$("#track").on("click", function() { whatareyousingingpatrick(); });'>
Get sniper for 15$
</button>
<br>
<button onclick='$("#track").on("click", function() { whatareyousingingpatrick2(); });'>
Get bomb for 25$
</button>
最佳答案
在
setInterval(function(){
dodat();
}, 1500-haha);
1500-haha
部分仅计算一次,这意味着 -haha
没有用。
如果您希望每次调用的时间间隔减少,您可以使用 setTimeout :
(function doone(){
dodat();
setTimeout(doone, 1500-haha); // a halting condition here would be nice
})();
关于javascript - SetInterval 不从间隔递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606164/
这个问题已经有答案了: JavaScript closure inside loops – simple practical example (45 个回答) 已关闭 8 年前。 我试图创建多个 se
这是我的情况,我需要加快函数运行时间,所以 setInterval 不是一个明智的选择,对吧?因为每次至少要花费 4 毫秒。 所以,我可以将 setInterval 函数更改为 requestAnim
我正在尝试下面的代码,看看是否可以将 setInterval Id 存储为关联数组中唯一键的值,然后通过使用唯一值作为键来停止被调用函数中的间隔,如下所示我将 setInterval Id 作为它的值
我花了很长时间试图弄清楚如何使用具有 CSS 样式缓动功能的 Google Maps API 为折线上的符号设置动画。我让它工作 with this Gist和 this Google Maps AP
我是这里的 JS 新手,正在研究一个在给定时间段后开始的倒数计时器。基本上,当用户单击按钮时时钟开始,第二个时钟在第一个计时器用完时开始。我知道“setInterval”是完成这个的最好方法。我面临的
嗨, friend 们,我想只使用一个 setInterval 函数来运行我的代码。目前我正在使用两个 setInterval's 来实现我的目标,我们可以只使用一个 'setInterval' 来获
我的“setInterval”函数有问题,我想在将鼠标悬停在 div 上时启动该函数,但是 setInterval 在我将鼠标悬停在 div 上时第一次起作用=>如果我停留在div,它不会继续改变图片
我想展示两次:一次在你的国家,一次在日本用JS 问题是第二个 setInterval 停止了第一个,我不知道如何进行两次运行。 完整代码 In your region:
好吧,这个问题有几个问题。 首先,我要求 setTimeout() 和 setInterval() 我见过几种不同的调用方式,我想知道哪种方式最适合这种情况。 我正在制作一个 js/canvas 游戏
setInterval(function () { //======= //code //======== if(--timer&etype="; } },1000); 这里定时器结束后,而不是重定
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我的前老板有一个 weird bug where when he used setInterval with a long delay interval : setInterval(func, 300
这个问题已经有答案了: How does the "this" keyword work, and when should it be used? (22 个回答) How to access the
我的印象是 setInterval("/*some code*/", time) 相当于 setInterval(function() { /*some code*/ }, time) 显然不是
我对 JavaScript 和 NodeJS 非常陌生,我只是想了解 NodeJS 中的发射器模式。当我尝试使用 setInterval 函数每秒发出一个刻度事件时,程序似乎工作正常:-
我有一个简单的 setTimeout 函数,它在特定时间运行并且工作正常: var now = new Date(); var milliTillExec = new Date(now.getFull
在本教程中,您将借助示例了解 JavaScript setInterval() 方法。 在 JavaScript 中,可以在指定的时间间隔内执行一段代码。这些时间间隔称为定时事件。 有
Js: function cometConnect(){ $.ajax({ cache:false, type:"post", d
LE2。关于如何解决此问题的任何其他想法? 我有这段代码,但不知道为什么不能正常工作: $(function autorun() { if ($("#contactForm").is(":visibl
这个问题在这里已经有了答案: How to make a setInterval stop after some time or after a number of actions? (6 个答案)
我是一名优秀的程序员,十分优秀!