Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带变量的实时增量计数器_Javascript_Php_Html_Counter - Fatal编程技术网

Javascript 带变量的实时增量计数器

Javascript 带变量的实时增量计数器,javascript,php,html,counter,Javascript,Php,Html,Counter,我正在尝试创建一个实时计数器,可以显示实时翻译的单词数。我将输入起始年、当前时间、每年翻译的单词数,这个计数器将通过平均每年翻译的单词数显示实时计数。这样我就不必创建假时间间隔来更新此计数器 我在网上找到了这个片段,我正在考虑编辑它以满足我的需要: <html> <head> <style type="text/css"> div.cont { position: relative

我正在尝试创建一个实时计数器,可以显示实时翻译的单词数。我将输入起始年、当前时间、每年翻译的单词数,这个计数器将通过平均每年翻译的单词数显示实时计数。这样我就不必创建假时间间隔来更新此计数器

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

<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]

好的,我现在有了一个可行的解决方案,数字是有意义的。谢谢你帮我弄明白怎么做,我想我的数学技能甚至比我想象的还要差

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Live Word Count</title>
    <style type="text/css">
    .count {
        width: 100%;
        margin: 0 auto;
        padding-top: 10%;
        text-align: center;
    }
    </style>
    <?php
    $now = time();
    $start = mktime(0, 0, 0, 1, 01, 2004);
    $wordsPerYear = 1000;
    $totalDays = (($now - $start) / 86400); // number of a seconds in a day
    $count = $totalDays / 365 * $wordsPerYear;
    $count = round($count);
    ?>

    <script type="text/javascript">
        var totalWords = <?php print($count); ?>;
        function updateCounter() {
            totalWords = totalWords + 1; // need to do something here to update to the real word count instead of incrementing by 1 each second
            document.getElementById("carb").innerHTML=totalWords; }
    </script>
</head>

<body onload="setInterval('updateCounter()', 1000);">
    <div class="count">
            <span id="carb">
                Loading Word Count...
            </span>
    </div>
</body>
</html>
我只需要能够使用翻译的单词的真实值,而不是使用setInterval'updateCounter',1000进行虚假的实时增量,来实时更新这个数字


你能帮我整理一下吗

要不要用点压痕?谢谢Melih!我实际上没有单词计数功能;我设想给一个变量,过去十年平均每年的单词数,比如说10年或者其他。我有那个号码,如果需要的话,我希望以后能更新。我不明白你想更新什么。你想为那一年更新它吗?例如,在年底。或者您想更新浏览器屏幕以获取从其他来源获得的新值?对不起,我将给您一个精确的示例来说明:假设我想在10年前的2004年1月1日开始计数以简化计算。假设我每年翻译的单词数是10000,假设现在的时间是2014年1月1日午夜,此时此刻显示的数字将是100000。我用整数来简化,但它们可以是任何数字,我的网站上任何时间点显示的数字都是从开始日期到今天翻译的单词的平均值。另一个例子,你能解释一下在这种情况下会发生什么:开始日期是2004年1月1日午夜,结束日期是2014年5月30日四舍五入,一年中的第150天午夜。每年翻译的单词数为10000。我不明白的是,如果你有每年翻译的单词数量,为什么你要寻找一个函数来找到它?你已经有了。你到底想找到什么?我看到你只是将10.000乘以10,即2014-2004年。你的意思是要用php计算它?像这样的事情可能是什么$count=$totalDays/365*$wordsPerYear在这里$totalDays是给定开始日期和现在之间间隔内过去的天数。