超文本标记语言
<div class="counter" data-target="
<?php
$sql = "select Books_Not_Returned from booksnotreturned ";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$val = $row['Books_Not_Returned'];
$Int = (int)$val;
echo $Int;
?>
">0</div>
JS系统
// COUNTER
const counters = document.querySelectorAll('.counter');
const speed = 500; // The lower the slower
counters.forEach(counter => {
const updateCount = () => {
const target = +counter.getAttribute('data-target');
const count = +counter.innerText;
// Lower inc to slow and higher to slow
const inc = target / speed;
// console.log(inc);
// console.log(count);
// Check if target is reached
if (count < target) {
// Add inc to count and output in counter
counter.innerText = count + inc;
// Call function every ms
setTimeout(updateCount, 1);
} else {
counter.innerText = target;
}
};
updateCount();
});
我试图使计数器停止在数据库中存储的值,当我试图将目标值分配给该变量时,它开始为1.00000000,然后为1.10000,依此类推,直到该变量的实际值
1条答案
按热度按时间jvlzgdj91#
实际上,在js代码中,我分配const inct = target/speed,因此inc被分配了一个双精度值,以增加延迟,它必须将inc减少到小数,我通过类型转换target/speed修复了它,即将int分配给inc