gpt4 book ai didi

php - 捕获javascript秒表停止时间并将其存储为php变量

转载 作者:行者123 更新时间:2023-11-29 06:17:37 25 4
gpt4 key购买 nike

我正在使用这个秒表:

<script language="JavaScript" type="text/javascript">
window.onload = function()
{
stopwatch('Start');
}

<!--
var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
sec++;
if (sec == 60) {
sec = 0;
min = min + 1; }
else {
min = min; }
if (min == 60) {
min = 0;
hour += 1; }

if (sec<=9) { sec = "0" + sec; }
document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;

if (text == "Start") { document.clock.theButton.value = "Stop "; }
if (text == "Stop ") { document.clock.theButton.value = "Start"; }

if (document.clock.theButton.value == "Start") {
window.clearTimeout(SD);
return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
sec = -1;
min = 0;
hour = 0;
if (document.clock.theButton.value == "Stop ") {
document.clock.theButton.value = "Start"; }
window.clearTimeout(SD);
}
// -->
</script>

并且想要捕获时钟停止的时间,然后将其存储为 PHP 变量,以便我可以将其与其他 PHP 变量加载一起插入到我们的数据库中。这可能吗?

感谢您的帮助

最佳答案

使用一些 AJAX 为您的代码增添趣味。在函数 resetIt() 内部将当前时间戳传递给您的 php 脚本。

jQuery 有一个可靠的 AJAX部分和很好的文档以及示例。

(假设 jQuery 已加载、启动并运行)

function resetIt() {
$.ajax({
url: 'your.php',
success: function(response){
alert(response);
}
});
sec = -1;
min = 0;
hour = 0;
if (document.clock.theButton.value == "Stop ") {
document.clock.theButton.value = "Start"; }
window.clearTimeout(SD);
}

your.php(因为您需要保存实际时间戳,所以我们不会将任何变量传递给 PHP 部分。如果您需要添加特定变量(来自 JS),当然可以添加它们)

if(mysql_query("INSERT INTO `database` (`stopped`) VALUES (NOW())")) {
echo 'success';
} else {
echo 'failed';
}
die();

关于php - 捕获javascript秒表停止时间并将其存储为php变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5536690/

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