由于缺少时间间隔,您的Fiddle链接对我无效,应该也使用getElementById
(仅因为它在Internet Explorer中有效并不意味着它是跨浏览器的)。
这里,我复制了它,建议您使用这个:
http://jsfiddle.net/spechackers/hJhCz/
我还清理了您第一个链接中的代码:
<pre id="p">
<script type="text/javascript">
var charMap=['p','.'];
var n=0;
function myInterval()
{
n+=7;//this is the amount of screen to "scroll" per interval
var outString="";
//this loop will execute exactly 4096 times. Once for each character we will be working with.
//Our display screen will consist of 32 lines or rows and 128 characters on each line
for(var i=64; i>0; i-=1/64)
{
//Note mod operations can result in numbers like 1.984375 if working with non-integer numbers like we currently are
var mod2=i%2;
if(mod2==0)
{
outString+="\n";
}else{
var tmp=(mod2*(64/i))-(64/i);//a number between 0.9846153846153847 and -4032
tmp=tmp+(n/64);//still working with floating points.
tmp=tmp^(64/i);//this is a bitwise XOR operation. The result will always be an integer
tmp=tmp&1;//this is a bitwise AND operation. Basically we just want to know if the first bit is a 1 or 0.
outString+=charMap[tmp];
}
}//for
document.getElementById("p").innerHTML=outString;
}
myInterval();
setInterval(myInterval,64);
</script>
</pre>
你提供的两个链接中的代码结果非常不同,但是它们的逻辑非常相似。两者都使用for循环来遍历所有字符、对非整数进行取模运算和按位异或运算。
它到底是如何工作的呢?基本上,我能告诉你的就是注意随着输入和输出变化而改变的变量。
所有的逻辑似乎都是一些比特加密方式来决定在页面中添加哪个2个字符或一个换行符。
从微积分或三角学的角度来看,我自己也不太理解。
var tmp=(mod2*(64/i))-(64/i)
(在else从句中)。但是我们需要乘以mod2(它将等于1)吗? - Parth Thakkarhttp://en.wikipedia.org/wiki/Linear_algebra#Linear_transformations
http://www.youtube.com/user/khanacademy/videos?query=linear+algebra