I was under the impression that
setInterval("/*some code*/", time)
was equivalent to
setInterval(function() {
/*some code*/
}, time)
Apparently not! Please compare the following (full HTML):
<pre id=p><script>n=setInterval("for(n+=7,i=k,P='p.\\n';i-=1/k;P+=P[i%2?(i%2*j-j+n/k^j)&1:2])j=k/i;p.innerHTML=P",k=64)</script>
and
<pre id=p><script>n=setInterval(function() { for(n+=7,i=k,P='p.\\n';i-=1/k;P+=P[i%2?(i%2*j-j+n/k^j)&1:2])j=k/i;p.innerHTML=P },k=64)</script>
The two animations (the first being taken from here) are different.
Why are the two constructs not equivalent?
Answer: There are at least three differences
- Variable scope
- Performance
- String character escapes
Those two forms of setInterval are essentially equivalent — but your code isn’t. In the second example you’re double-escaping your newline as
\\n, instead of just\n. Try this:And that should be just like what you want.
The
function()form of setInterval is better in a lot of ways — it’s more readable, and as you ran into here, you don’t have to deal with trying to escape strings inside of string.