0

i have tried to execute this simple code, about CPS. This works in Chrome 43, but not in Firefox and Opera ... what's wrong ? (s.o. Linux Mint 17 )

(function(){

  var i = 0;
  function forloop(){
    if(i<10){
      document.write(i)
      i++; 
      setTimeout(forloop, 0);
    }
  }

  forloop();
})();
  • 2
    What doesn't work? Just tested on Firefox Dev Edition and it's okay, it logs all the `i` (I replaced your write with a `console.log`). – Kyll Jul 29 '15 at 21:51
  • What do you expect the code to do and what does it do in Firefox? – Felix Kling Jul 29 '15 at 21:52
  • in firefox does not appear all digit, but show only 0, then execution stop –  Jul 29 '15 at 21:52
  • 2
    Don't ever use `document.write` in an asynchronous function. – Bergi Jul 29 '15 at 21:56
  • ok but does not explain, beacause in Chrome works ... and no, in other two browser ... about console , i have tried all works good in all browser ! –  Jul 29 '15 at 21:58
  • 1
    `document.write` seems like one of those old things still in JavaScript because of JavaScript reasons. Using the cleaner `console.log` works okay. – Kyll Jul 29 '15 at 22:01
  • Just for the reference: [Why is document.write considered a “bad practice”?](http://stackoverflow.com/q/802854/1048572). If this has helped you, I'm inclined to close as a duplicate. – Bergi Jul 30 '15 at 12:14

1 Answers1

0

Well, the answer is: document.write smells bad and seems like a remnant of dark forgotten pasts.
Don't use that. Use console.log, or even append to some HTML, but not document.write.

This works perfectly across all normal browsers :

(function(){

  var i = 0;
  function forloop(){
    if(i<10){
      console.log(i);
      i++; 
      setTimeout(forloop, 0);
    }
  }

  forloop();
})();
Kyll
  • 6,830
  • 6
  • 39
  • 56