Search code examples
jquerymathsetintervalroundingfloor

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


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


Solution

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