是否可以使用javascript延迟页面拼写单词?

Mar*_*ody 4 javascript jquery

奇怪的问题我知道.我试图在一个页面上逐字加载一个小字延迟.是的,这是我在工作时编写的有时候的想法.我尝试了很多方法,但绝对没有成功.我得到的最好的是一个警告框,其中包含以下代码,但我想在页面上使用html进行操作.这甚至可能吗?

<script type="text/javascript">

var foo = "foo bar";
 foo = foo.length ;
for(i= 1; i<=foo; i++){
    var hello = "FOO BAR";
    hello = hello.substring(0, i);      
    alert(hello);

}

</script>
Run Code Online (Sandbox Code Playgroud)

我假设必须有一些类型的设置超时与隐藏节目和div加载它?

Vin*_*nie 11

你可以尝试这样的事情:

  var word = "Hello World";

   function nextChar(i){
      $("#word").text("The Word is: [" + i + "]" + word.substring(0, i));
      if(i < word.length){
        setTimeout("nextChar(" + (i + 1) + ")", 1000);   
      }
   }

  $(document).ready(function(){
    nextChar(0);
  });
Run Code Online (Sandbox Code Playgroud)

和HTML:

  <div id="word"></div>
Run Code Online (Sandbox Code Playgroud)


Ada*_*kis 6

假设您要将"foo bar"加载到此div中,一次加载一个字符,中间延迟1秒.

<div id="destination" />

$(function () {
    loadWord("foo bar", 0);
});

function loadWord(word, index) {
   if (index == word.length) return;
   $("#destination").text($("#destination").text() + word[index]);
   setTimeout(function() { loadWord(word, index + 1); }, 1000);
}
Run Code Online (Sandbox Code Playgroud)