如何解决帮助停止setInterval
| 不太确定如何设置。我试图做互联网告诉我的事情。<?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>
解决方法
您在
setInterval
中声明了一个动态函数,对该函数内的ѭ2call的调用将无法访问thisFunc
变量。
尝试声明该函数不在ѭ1的范围内,如下所示:
/// 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>\';
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。