gpt4 book ai didi

javascript - 创建计时器/秒表并在动画后启动

转载 作者:行者123 更新时间:2023-11-28 17:23:03 25 4
gpt4 key购买 nike

您好,我正在做一个元素,我一直在考虑是否可以在动画结束后启动计时器,并在按键时停止计时器?然后将时间结果发送到另一个页面?对于计时器,我指的是秒表,所以没有计数器或其他任何东西......

这里是原始代码:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Exercise1</title>
<style>
.first-child {
width: 200px;
height: 200px;
background: white;
margin-top: 150px;
margin-bottom: 50px;
margin-right: 0px;
margin-left: 550px;
-webkit-animation: myfirst 1s;
animation: myfirst 1s;
}
@-webkit-keyframes myfirst {
0% {background: white;}
20% {background: white;}
40% {background: white;}
60% {background: white;}
80% {background: white;}
100% {background: red;}
}

.first-parent {
color: blue;
margin-top: 5px;
margin-bottom: 50px;
margin-left: 600px;
margin-right: 0px;
}
.second-parent {
color: red;
margin-top: 0px;
margin-bottom: 50px;
margin-left: 40px;
margin-right: 0px;
}
p {
margin-left: 640px;
}
</style>
</head>
<body>

<div class='first-child'></div>

<button class='first-parent' onclick="window.location.href='Exercise2.html' ">B</button>

<button class='second-parent' onclick="window.location.href='Exercise2.html' ">R</button>

<br />
<p>1/2</p>

<script>
document.onkeypress = function(b) {
b = b || window.event;
var charCode = b.charCode || b.keyCode,
character = String.fromCharCode(charCode);

console.log(charCode);
window.location.href="Exercise2.html";
};

document.onkeypredss = function(r) {
r = r || window.event;
var charCode = r.charCode || r.keyCode,
character = String.fromCharCode(charCode);

console.log(charCode);
window.location.href='Exercise2.html';
};

</script>
</body>
</html>

如您所见,我还没有任何计时器...对此表示抱歉,但如果有任何问题,请尽管提问,我最熟悉 HTML、CSS、JavaScript 并且我知道一些 jQuery,但如果还有其他问题这样做的方法我也很高兴听到。提前致谢,和平!

最佳答案

好吧,既然你已经标记了 jQuery,我会给你一个 jQuery 解决方案。

可以使用以下 JavaScript/jQuery 创建秒表:

var h1 = document.getElementsByTagName('h1')[0],
seconds = 0,
t;
function add() {
seconds++;
h1.textContent = seconds;
timer();
}
function timer() {
t = setTimeout(add, 1000);
}

然后我们还需要定义一个 start() 和一个 stop() 函数来启动和停止计时器:

// start and stop functions
function start() {
timer();
}

function stop() {
clearTimeout(t);
}

就是这样!现在我们已经创建了一个秒表。

在您的问题中,您提到您希望秒表在动画结束后开始。我们现在可以通过在动画的回调函数中调用 .start() 来做到这一点,如下所示:

// Example of an animation that starts the timer when complete
$("#animatedDiv").animate({
left: "+=50",
top: "+=150",
height: "100px",
width: "200px"
}, 4000, function() { // define a callback function

// Animation complete.
start(); // Start the timer in the callback function
});

同样,我们也可以在按键时stop()计时器:

// Example of a function that stops the timer
$(document).keypress(function( event ) {
if ( event.which == 115 ) { // when pressing s for 'stop'
alert('You stopped the timer');
stop();
}
});

现在当我们想要将秒表的时间发送到另一个页面时,我们需要在指向另一个页面时将计时器的值添加为 url 参数。

一般来说,这是按如下方式完成的:

http://www.newpage.com/?myParameter=myValue

我们发送包含值 myValue 的变量 myParameter

在您的示例中,我们可以按如下方式执行此操作:

// button click function
$("#theButton").click(function() {
window.open('http://www.yourpage.com/?time='+h1.textContent);
});

h1.textContent 将是计时器的值。然后另一个页面可以从 url 获取变量 time。如果您不知道如何执行此操作,请阅读以下内容:

我已将所有这些放在一个易于理解的 jsFiddle 中:

jsFiddle DEMO

通过该演示,应该可以实现您想要实现的目标。

enter image description here

关于javascript - 创建计时器/秒表并在动画后启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27678604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com