动态的打字效果,代码不解!请教!!!

renaski 2003-09-11 07:49:50
<script type=\\\"text/JavaScript\\\">
<!--
var txt = \\\"大家请看,这里就是效果\\\";
var chars = txt.length + 1; //应该是11+1=12=chars
var newstatus = \\\"\\\";
var i = 0;

function txtinstatus()
{
if (i < chars) //i小于12
{
setTimeout(\\\"nextletter()\\\", 200);
}
}

function nextletter()
{
if ((i+1)>=chars) // 不懂的地方,i+1>=12不是初始值只有0吗?
{
newstatus = txt.substring(0,i)
}
else //怎么会ELSE呢?
{
newstatus = txt.substring(0,i)+\\\"■\\\";
}
window.status = newstatus;
i++;
txtinstatus();
}
// -->
</script>

问题在我第一个循环就是0+1=1=i
1>=12??? 怎么会有循环呢?newstatus = txt.substring(0,i)
...全文
38 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
windancer 2003-09-12
  • 打赏
  • 举报
回复
function nextletter()
{
if ((i+1)>=chars) //就是说如果是最后一个字,那么就不用出现■
{
newstatus = txt.substring(0,i)
}
else //如果不是最后一个字,那么就在最后跟上一个■
{
newstatus = txt.substring(0,i)+"■";
}
====================================================
而且先是字先打出,然后■在出来的。并且是交替出现的。
==================================================
你还没有理解思路,或者说你没有理解substring的用法?!

加上这两句调试语句是否有助于你理解
alert(i)
alert(newstatus)

window.status = newstatus;
i++;
renaski 2003-09-12
  • 打赏
  • 举报
回复
ricky460(阿酷) 说的对,
首先更正不是交替出现的,我就是因为这样想所以思路就变的死循环了。谢谢个位了
ricky460 2003-09-12
  • 打赏
  • 举报
回复
程序没有错啊..楼主搞错了...

if ((i+1)>=chars)//假设I=12+1>=12

这一句一开始的时候i = 0 ; i +1 = 1 ; 1 < 12 ...
所以一开始一直调用的是 else里的语句newstatus = txt.substring(0,i)+"■";
在打印效果里,"■"表示还有没有输入的字 . . .
当i = 12 ; i +1 = 13 ; i > 12 时 ...
就一直调用newstatus = txt.substring(0,i)这句 . . .
因为所有的字都显示出来了..所以不需要再显示"■"了....
所以总体显示效果是:
大■
大家■
大家请■
大家请看■
大家请看,■
大家请看,这■
大家请看,这里■
大家请看,这里就■
大家请看,这里就是■
大家请看,这里就是效■
大家请看,这里就是效果
renaski 2003-09-11
  • 打赏
  • 举报
回复
而且先是字先打出,然后■在出来的。并且是交替出现的。
如果function nextletter()
{
if ((i+1)>=chars)
{
newstatus = txt.substring(0,i)
}
else //这个ELSE总是看不懂。怎么会有"■"这个呢???
{
newstatus = txt.substring(0,i)+"■";
}
谢谢哪位能帮我一下。我的思路都被搞糊涂了,谢谢啊!!!
renaski 2003-09-11
  • 打赏
  • 举报
回复
function nextletter()
{
if ((i+1)>=chars)//假设I=12+1>=12 #1
{
newstatus = txt.substring(0,i)
}
else //这里也就是说I<12 #2
{
newstatus = txt.substring(0,i)+"■";
}
window.status = newstatus;
i++;

问题就在着#2 不是I++吗?那么函数递归后怎么还会<=12呢?就是说没有ELSE了呀
照我的看法是永远是>=12的因为是++吗!!!
WYlslrt 2003-09-11
  • 打赏
  • 举报
回复
它是判断当它>=12时,才怎么样,如果不是就再怎么样。
wjcking 2003-09-11
  • 打赏
  • 举报
回复
<script type=\\\"text/JavaScript\\\">
<!--
var txt = \\\"大家请看,这里就是效果\\\";
var chars = txt.length + 1; //应该是11+1=12=chars
var newstatus = \\\"\\\";
var i = 0;

function txtinstatus()
{
if (i < chars) //i小于12
{
setTimeout(\\\"nextletter()\\\", 200);//200毫秒调用底下的函数
}
}

function nextletter()
{
if ((i+1)>=chars) // 不懂的地方,i+1>=12不是初始值只有0吗?
//再看看底下不是还有个i++呢吗?
{
newstatus = txt.substring(0,i)
}
else //怎么会ELSE呢?
{
newstatus = txt.substring(0,i)+\\\"■\\\";
}
window.status = newstatus;
i++;//这里 再看看上面那个函数,隔一段时间就递归调用这个函数调用一次不久加一次,然后条件不久符合了?
txtinstatus();
}
// -->
</script>

问题在我第一个循环就是0+1=1=i
1>=12??? 怎么会有循环呢?newstatus = txt.substring(0,i)
renaski 2003-09-11
  • 打赏
  • 举报
回复
没人呀,CSDN怎么没有高手啊?

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧