Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript ForEach Function does not work in IE

how could I write the following Code that it is supported in all browsers? Because it seems that the forEach-Function is not supported in IE8...

    digits.forEach( function( value, index ) {
    // create a span with initial conditions
    var span = $( '<span>', {
        'class': 'digit0',
        'data': {
            'current': 0,
            'goal' : value
        }
    } );
    // append span to the div#number
    span.appendTo( $( 'div#number' ) );
    // call countUp after interval multiplied by the index of this span
    setTimeout( function() { countUp.call( span ); }, index * interval );
} );

See the full Code here: http://jsfiddle.net/bBadM/ (it´s not working with all browsers) Thanks in advance.

Regards,

like image 538
Simon Avatar asked Dec 12 '25 13:12

Simon


1 Answers

The MDN documentation for forEach includes two implementations of the method for use in browsers that implement earlier versions of JS.

I'll reproduce the quick one (see the link for the complete one) here:

if ( !Array.prototype.forEach ) {
  Array.prototype.forEach = function(fn, scope) {
    for(var i = 0, len = this.length; i < len; ++i) {
      fn.call(scope, this[i], i, this);
    }
  }
}
like image 111
Quentin Avatar answered Dec 15 '25 02:12

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!