Question

  fadeIn:function(speed,till){
          speed = speed !== undefined ? speed : 400;
          speed = typeof speed === 'number'  ? speed : speed == "slow" ? 600 : speed == "fast" ? 200 : speed !== 'slow' || speed!== 'fast' ? 400 : 400;
          var opc = window.getComputedStyle(this[0]).opacity;
          var element = this[0];
          var add = 100 / speed;
          till = till !== undefined ? till : 1;
          element.style.display="block";
          var timer = (function  a() {
           if (opc >= till ){         
            element.style.opacity = 1;              
            clearInterval(timer);
            return false;
            }
            if(browser[3] == 'MSIE' && Number(browser[4]) < 9 ){
            add = 10000 / speed;
            element.style.filter = 'alpha(opacity="' + (opc+ add) + '")';
            }
            element.style.opacity = opc;
            opc = opc + add; // code here where addition starts
            setTimeout(a, 50);
          })(); 
    },

Now the function above will not run its course correctly. As JavaScript does not correctly add floating decimals. So I've tried many ways...

 opc = (opc * 100) + (add * 100) / 100;

Also tried a close hack as this gives me my decimal number the above gives me a whole number no matter what really.

 opc = "."+parseInt((opc * 100) + (add * 100) / 100,10);

Though I believe whats wrong with the above is that it turns into a string and well just doesn't cut. So the question is how do I increment decimals as this is very necessary.

EXAMPLE

  0.001 - 0.002 - 0.003 - 0.004 etc 
Was it helpful?

Solution

JavaScript is adding floating point numbers fine.
Probably one of your operands is not a floating point number.

Quote from a JS book: JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java’s double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value.

I would try to debug the code and then see which values are strings (or not numbers), and then parse them using the parseFloat function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top