gpt4 book ai didi

javascript - 带变量的实时增量计数器

转载 作者:行者123 更新时间:2023-11-28 08:27:29 25 4
gpt4 key购买 nike

我正在尝试创建一个实时计数器来显示实时翻译的字数。我会向它提供开始年份、当前时间、每年翻译的单词数,并且该计数器将通过平均每年翻译的单词数来显示实时计数。这样我就不必创建假时间间隔来更新此计数器。

我在网上找到了这个片段,我正在考虑编辑它以满足我的需要:

<html>
<head>
<style type="text/css">
div.cont {
position: relative;
background-image: url(counter.gif);
width:160px;
height:110px;
vertical-align:text-bottom;
}
div.cont div.ans {
position: absolute;
bottom: 0px;
margin-bottom:15px;
margin-left:7px;
color:black;
font-family: Verdana, Tahoma, Sans-Serif;
font-size: 15pt;
line-height: normal;
}
</style>
<?php
$now=time();
$start=mktime(0, 0, 0, 1, 24, 2007);
$carbonsaving=((($now - $start) * 0.0058774) + 130000);
$format=round($carbonsaving, 2);
// in this example
// $now=a unix timestamp of this very second
// $start is the date that you want the counter to start from sent over
//as a unix timestamp
// $carbonsaving is the calculation that you want to perform to get
//your base figure
// i.e. total saving=( (date now - start date)* growth rate) + base rate
// this gives us the starting saving all that needs to be done is increment it with javascript
?>
<script type="text/javascript">
// we need to import our server side variable into javascript to let it increment live
var car = <? php print($format); ?> ;
var rou

function incs() {
car = car + 0.01;
rou = Math.round(car * 100) / 100
document.getElementById("carb").innerHTML = rou;
}
// what function incs does is take car and adds 0.01 to it
//rou rounds the figure to 2 dp
//the document.getElementById("carb") can refer to a <p> tag //<span> or whatever and just says with .innerHTML=rou; that the //value between the results of rou
</script>
</head>
<!-- body onload setInterval tells the page to load our javascript function and repeat it by every x microseconds, so this repeats every 2 seconds //-->

<body onload="setInterval('incs()', 2000)">
<div class="cont">
<div class="ans"> <span id="carb">Calculating...</span>

</div>
</div>
</body>
</html>

谁能帮我做到这一点,因为我对 PHP 太差了......谢谢

最佳答案

var start = <?php echo mktime(0, 0, 0, 1, 24, 2007); ?>; // desired start time.
// refer to [1] for mktime()
function updateCounter(){
$(function(){
$( "#count" ).load( "count.php?stime="+start );
});
}

它将从 php 获取当前的字数并写入 id。

您现在可以设置更新间隔:

<body onload="setInterval('updateCounter()', 2000)">

PHP:

<?php
// I assume that you have the function that finds translated words per year.
// It's not clear that what you want from server side and client site.
// Update me, please. Then I will update for you.
echo $counter->get_current($_GET['stime']);
?>

[1] http://tr1.php.net/manual/en/function.mktime.php

关于javascript - 带变量的实时增量计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22240077/

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