jQuery 使用 setInterval() 的 Math.round() 和 Math.floor() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5438097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math.round() and Math.floor() problem with setInterval()
提问by Sayed
I'm having problem with Math.round() and Math.floor() in setInterval() function by using jQuery.
我在使用 jQuery 时在 setInterval() 函数中遇到 Math.round() 和 Math.floor() 问题。
This is my code:
这是我的代码:
var number1 = 400;
var up_up = setInterval(
function (){
number1 = parseFloat(number1) + parseFloat(0.2548777);
number1 = Math.round(number1);
$('#number1').html(number1);
}, 1000);
The Math.round() or Math.floor() doesn't work, but when I use Math.ceil() it works fine, but I want round or floor..
Math.round() 或 Math.floor() 不起作用,但是当我使用 Math.ceil() 时它工作正常,但我想要 round 或 floor..
Please help
请帮忙
回答by RSG
When you say 'doesn't work' you mean always rounds down and keeps the value of number1 at 400 indefinitely. You need to save the rounded value to a different variable or assign it directly to your display field. :)
当您说“不起作用”时,您的意思是始终向下取整并将 number1 的值无限期地保持在 400。您需要将舍入后的值保存到不同的变量或直接将其分配给您的显示字段。:)