我试图在屏幕上逐渐显示文字(如选框)。例如H ..他......地狱..你好。当我在VS2010的调试中跟踪它时,它正在工作!但是当它实际运行时,它会立即显示整个句子。
我在每个字母之间做了一个“延迟”约3秒,所以它会假设需要一段时间,但实际上它会立即显示所有内容。
谁是解开这个谜团的天才? (请不要给我建议如何创建选框效果,这不再是问题了。现在它只是我和javascript之间的一个WAR!)我假设它在从函数调用函数时与同步有关吗?
感谢任何帮助我恢复理智的人。
你可以从这里下载代码(VS项目): http://pcgroup.co.il/downloads/misc/function_from_function.zip
或在此查看:
<body>
<script type="text/javascript">
//trying to display this source sentence letter by letter:
var source = "hi javascript why are you being such a pain";
var target = "";
var pos = 0;
var mayGoOn = false;
//this function calls another function which suppose to "build" the sentence increasing index using the global var pos (it's even working when following it in debug)
function textticker() {
if (pos < source.length) {
flash();
if (mayGoOn == true) {
pos++;
mayGoOn = false;
document.write(target);
textticker();
}
}
}
function flash() {
//I tried to put returns everywhere assuming that this may solve it probably one of them in not necessary but it doesn't solve it
if (mayGoOn == true) { return; }
while (true) {
var d = new Date();
if (d.getSeconds() % 3 == 0) {
//alert('this suppose to happen only in about every 3 seconds');
target = source.substring(0, pos);
mayGoOn = true;
return;
}
}
}
textticker();
</script>
答案 0 :(得分:0)
你显然做错了。看看这个。
var message = "Hello World!";
function print(msg, idx) {
if(!idx) {
idx = 0;
}
$('#hello').html(msg.substring(0, idx));
if(idx < msg.length) {
setTimeout(function() { print(msg, idx + 1) }, 200);
}
}
print(message);