gpt4 book ai didi

javascript - ajax php jquery实时保存计数器

转载 作者:行者123 更新时间:2023-11-30 15:56:45 25 4
gpt4 key购买 nike

目前 script.php 工作完美,但我想要发生的事情不是显示小时、分钟和秒,而是我想将 3 个值组合起来并作为 $_POST['timeSpent '] 以及每 1 秒作为 $_POST['id'] 发送的值 $id。

脚本.php

<p><span id="hours"></span>:<span id="minutes"></span>:<span id="seconds"></span></p>
<script>
var sec = -1;
function pad(val) { return val > 9 ? val : "0" + val; }
setInterval(function () {
$("#seconds").html(pad(++sec % 60));
$("#minutes").html(pad(parseInt(sec / 60, 10) % 60));
$("#hours").html(pad(parseInt(sec / 3600, 10)));
}, 1000);
</script>

日志.php

<?php
include 'includes/config.php';

$loggeduser = $_SESSION['username'];
$stmt = "UPDATE rotator_tracking SET time_spent=:time_spent WHERE id=:id";
$stmt = $db->prepare($stmt);
$stmt ->execute(array(
":time_spent" => $_POST['timeSpent'],
":id" => $_POST['id']
));
?>

我知道需要将变量传递给 ajax,ajax 将它们传递给 log.php,但在这种情况下,我不知道该怎么做,也不知道如何每 1 秒保存一次。

帮助?也许是示例代码。

最佳答案

在你的 setInterval 循环中只需添加

$.post(
'log.php',
{
timeSpent:theTimeCombinedVariable,
id:IdVariable
}
).done(function() {
alert('done');
});

我没有测试代码。但仅此而已。

关于javascript - ajax php jquery实时保存计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38403433/

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