Math.round() and Math.floor() problem with setInterval()

Sayed picture Sayed · Mar 25, 2011 · Viewed 50.1k times · Source

I'm having problem with Math.round() and Math.floor() in setInterval() function by using jQuery.

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..

Please help

Answer

RSG picture RSG · Mar 25, 2011

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. :)