dongwei4444 2015-08-20 19:13
浏览 39

如何每秒为函数添加1?

I have a function called $function. I want my code to add 1 to $function every second, and in "live".

$function = 100;
$add = $function + 1;

echo $add;

How do I run/execute $add every second? I would like to see the number getting bigger in "live" (without having to refresh the page every time I want to see the result).

  • 写回答

1条回答 默认 最新

  • douyangcheng4965 2015-08-20 19:22
    关注

    PHP is a server-side language which can only actually do anything on your server. If you want things to update dynamically on the client (i.e. your browser) you need a client-side language, like JavaScript. Generally server-side languages like PHP generate client-side code which is then transferred over a network to your browser.

    For example, you could get your PHP server to output the following HTML:

    <html>
        <body>
            <div id="number">1</div>
            <script>
                var text = document.getElementById("number");
                var number = 1;
    
                window.setInterval(function() {
                    // code in here will repeat every 1000ms
                    number = number + 1;
                    text.innerText = number;
                }, 1000);
            </script>
        </body>
    </html>
    

    If you're just learning to program, you might enjoy learning HTML and JavaScript more, without worrying about the server for now, as you can get to see the results of your program more dynamically. A good resource might be Codecademy or direct from Mozilla

    评论

报告相同问题?