gpt4 book ai didi

javascript - HTML/JS/在一个选项卡中花费的时间

转载 作者:行者123 更新时间:2023-11-29 07:21:31 25 4
gpt4 key购买 nike

我有一个非常具体的问题。我浏览了整个 Internet 以寻找解决方案,但没有找到任何有用的信息。

我有这个 HTML 代码:

    <form action="/timer.php" method="POST">
<span>First Name:</span><input name="firstname" type="text" />
<input type="submit" value="Submit">
</form>

当我通过输入我的名字进入 timer.php 站点时,新选项卡应该计算在此页面上花费的时间,并使用我输入的名字将其保存在任何地方(文件或数据库)并每半小时自动保存一次。

当前 html

<html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>

function call_ajax(pageOpen, firstname){
pageClose =new Date();

//count spent minutes and seconds on page
minutes= pageClose.getMinutes() - pageOpen.getMinutes();
seconds= pageClose.getSeconds() - pageOpen.getSeconds();

//send the spent minutes, seconds and firstname to timer.php file
$.ajax({
url: "timer.php",
data: {'minutes': Math.abs(minutes), 'seconds':Math.abs(seconds), 'firstname':firstname}
})

}

$(document).ready(function() {
//When open the page get the current time
var pageOpen = new Date();

//call the ajax to send the request in timer.php every 30 min
setInterval(function(){var firstname = $('#firstname').val();call_ajax(pageOpen, firstname); }, 1000 * 60 * 30);

$("button").click(function(e){
e.preventDefault();
//call the ajax to send the request in timer.php on click the button
var name = $('#firstname').val();
call_ajax(pageOpen, name);
});
});

</script
</head>
<body>
<form action="/timer.php" method="POST" name="timer" >
<span>First Name:</span><input name="firstname" type="text" id="firstname" />

<input type="submit" value="Submit">
</form>




</body>
</html>

我的 timer.php:

<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
$minutes = $_POST['minutes'];
$seconds = $_POST['seconds'];
?>


<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=Windows-1250" />

</head>
<body>



<
Meno Užívateľa: <b> <?= $firstName ?> </b>
</br>
</br>

Momentálne majníš : <b> <?= $minutes ?> Minút </b> <b> a </b> <b> <?= $seconds ?> Sekúnd </b>
</br>
</br>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Refresh">



</body>
</html>

最佳答案

使用 ajax 计算在页面上花费的时间。对于自动保存调用方法 setInterval。

这里是完整的代码。

idex.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>

function call_ajax(pageOpen, firstname){
pageClose =new Date();

//count spent minutes and seconds on page
minutes= pageClose.getMinutes() - pageOpen.getMinutes();
seconds= pageClose.getSeconds() - pageOpen.getSeconds();

//send the spent minutes, seconds and firstname to timer.php file
$.ajax({
url: "timer.php",
type: "POST",
data: {'minutes': Math.abs(minutes), 'seconds':Math.abs(seconds), 'firstname':firstname},
success: function(result){
$('#result').html(result);
}
})

}


$(document).ready(function() {
//When open the page get the current time
var pageOpen = new Date();

//call the ajax to send the request in timer.php every 30 min
setInterval(function(){var firstname = $('#firstname').val();call_ajax(pageOpen, firstname); }, 1000 * 60 * 30);


$("button").click(function(e){
e.preventDefault();
//call the ajax to send the request in timer.php on click the button
var name = $('#firstname').val();
call_ajax(pageOpen, name);

});
});




</script>
</head>
<body>

<!-- Form --->
<form name="timer" method="POST">
<span>First Name:</span><input name="firstname" type="text" id="firstname" />
<button>Submit</button>
</form>

<div id="result"></div>

</body>
</html>

获取timer.php文件中的ajax请求。并保存在数据库或文件中。

定时器.php

<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
$minutes = $_POST['minutes'];
$seconds = $_POST['seconds'];
?>
Meno Užívateľa: <b> <?= $firstName ?> </b>
</br>
</br>
Momentálne majníš : <b> <?= $minutes ?> Minút </b> <b> a </b> <b> <?= $seconds ?> Sekúnd </b>
</br>
</br>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Refresh">

关于javascript - HTML/JS/在一个选项卡中花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56093414/

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