从函数WEIRD RESULTS调用函数

Calling function from function WEIRD RESULTS

本文关键字:函数 调用 RESULTS WEIRD      更新时间:2023-09-26

我试图在屏幕上逐渐显示一个文本(比如marquee)。例如H.He。。地狱你好当我在VS2010的调试中跟踪它时,它正在工作!但当它实际运行时,它会同时显示整个句子。

我在每封信之间都做了大约3秒的"延迟",所以这可能需要一段时间,但实际上它会立即显示一切。

谁是解开这个谜的天才?(请不要给我如何创建字幕效果的建议,这已经不是问题了。现在这只是我和javascript之间的一场战争!)我假设从函数调用函数时必须与同步有关?

感谢任何能帮助我恢复理智的人。

您可以从这里下载代码(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>

你显然做错了。看看这个。

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);

演示:http://jsbin.com/evehus