帮助停止 setInterval

发布于 2024-11-17 06:03:34 字数 2092 浏览 0 评论 0原文

不太确定如何设置。我试着按照互联网告诉我的去做。

<?php

function animateFunds($startAmount, $finishAmount)
{
    echo '<div id="fundage" style="display:inline;"> ';
    echo number_format($startAmount, 2, '.', ',') . ' </div>';

    //we are shooting for about 5 seocnds of
    //animation total. So get total amount of money
    //we are going to cover, and figure out the
    //setInterval delay for JS
    $distanceInPennies = ($finishAmount - $startAmount) * 100;
    //5 secs in MS = 5000
    $delay = 5000 / $distanceInPennies;
    //round $delay
    $delay = floor($delay);
    //delay no less than 5 ms
    if($delay < 5) $delay = 5;

    /// countdown JS
echo '<script type="text/javascript">
        var thisFunc = setInterval(function () {

            //instantiate delay
            //delay = ' . $delay . ';

            // get current fundage
            var currentFund = document.getElementById("fundage").innerHTML;
            currentFund = parseFloat(currentFund);

            // add a penny
            currentFund += 0.01;
            // round to 2 decimal places
            currentFund = Math.round(currentFund * 100)/100;
            // dont update more than finish amount
            if(currentFund > ' . $finishAmount . ') currentFund = ' . $finishAmount . '; 

            //if finish amount reached, stop function
            if(currentFund = ' . $finishAmount . ') clearInterval(thisFunc);

            // update countdown div
            document.getElementById("fundage").innerHTML = currentFund;

        }, ' . $delay . ');
            </script>
        ';
    /// END countdown JS
}// END function    

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

blah blah blah $<?PHP animateFunds(2.00, 10.00); ?> after after after.

</body>
</html>

not quite sure how to set this up. I tried to do what the internetz tolded me.

<?php

function animateFunds($startAmount, $finishAmount)
{
    echo '<div id="fundage" style="display:inline;"> ';
    echo number_format($startAmount, 2, '.', ',') . ' </div>';

    //we are shooting for about 5 seocnds of
    //animation total. So get total amount of money
    //we are going to cover, and figure out the
    //setInterval delay for JS
    $distanceInPennies = ($finishAmount - $startAmount) * 100;
    //5 secs in MS = 5000
    $delay = 5000 / $distanceInPennies;
    //round $delay
    $delay = floor($delay);
    //delay no less than 5 ms
    if($delay < 5) $delay = 5;

    /// countdown JS
echo '<script type="text/javascript">
        var thisFunc = setInterval(function () {

            //instantiate delay
            //delay = ' . $delay . ';

            // get current fundage
            var currentFund = document.getElementById("fundage").innerHTML;
            currentFund = parseFloat(currentFund);

            // add a penny
            currentFund += 0.01;
            // round to 2 decimal places
            currentFund = Math.round(currentFund * 100)/100;
            // dont update more than finish amount
            if(currentFund > ' . $finishAmount . ') currentFund = ' . $finishAmount . '; 

            //if finish amount reached, stop function
            if(currentFund = ' . $finishAmount . ') clearInterval(thisFunc);

            // update countdown div
            document.getElementById("fundage").innerHTML = currentFund;

        }, ' . $delay . ');
            </script>
        ';
    /// END countdown JS
}// END function    

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0     Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

blah blah blah 
lt;?PHP animateFunds(2.00, 10.00); ?> after after after.

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2024-11-24 06:03:34

您在 setInterval 中声明一个动态函数,您在该函数内调用 clearInterval 将无法访问 thisFunc 变量。

尝试在 setInterval 范围之外声明该函数,如下所示:

/// countdown JS
echo '<script type="text/javascript">
    function doTimer() {

    //instantiate delay
    //delay = ' . $delay . ';

    // get current fundage
    var currentFund = document.getElementById("fundage").innerHTML;
    currentFund = parseFloat(currentFund);

    // add a penny
    currentFund += 0.01;
    // round to 2 decimal places
    currentFund = Math.round(currentFund * 100)/100;
    // dont update more than finish amount
    if(currentFund > ' . $finishAmount . ') currentFund = ' . $finishAmount . '; 

    //if finish amount reached, stop function
    if(currentFund = ' . $finishAmount . ') clearInterval(thisFunc);

    // update countdown div
    document.getElementById("fundage").innerHTML = currentFund;

}

var thisFunc = setInterval("doTimer()", ' . $delay . ');
</script>';

You're declaring a dynamic function in the setInterval, your call to clearInterval inside that function will not be able to access the thisFunc variable.

Try declaring the function outside the scope of the setInterval, like so:

/// countdown JS
echo '<script type="text/javascript">
    function doTimer() {

    //instantiate delay
    //delay = ' . $delay . ';

    // get current fundage
    var currentFund = document.getElementById("fundage").innerHTML;
    currentFund = parseFloat(currentFund);

    // add a penny
    currentFund += 0.01;
    // round to 2 decimal places
    currentFund = Math.round(currentFund * 100)/100;
    // dont update more than finish amount
    if(currentFund > ' . $finishAmount . ') currentFund = ' . $finishAmount . '; 

    //if finish amount reached, stop function
    if(currentFund = ' . $finishAmount . ') clearInterval(thisFunc);

    // update countdown div
    document.getElementById("fundage").innerHTML = currentFund;

}

var thisFunc = setInterval("doTimer()", ' . $delay . ');
</script>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文