jQuery / JavaScript只执行第一条指令

时间:2013-12-05 13:42:29

标签: javascript jquery html

我有一些函数,然后是一个名为main();的函数,它应该以给定的顺序执行所述函数,并在其间进行休眠。

在我的HTML上,<body onload="main();">就像它应该的那样。

这是我的JavaScript文件:

  ctrl = 0;
  y = 0;
  block = 0;
  test = "";
  first_time = 1;    

  function typing(id, sentence) {
      var result = $.Deferred();
      var index = 0;
      var intObject = setInterval(function () {
          document.getElementById(id).innerHTML += sentence[index];
          index++;
          if (index == sentence.length) {
              clearInterval(intObject);
          }
      }, 100);
      return result.promise();
  }

  function paragraph(x) {
      var result = $.Deferred();
      while (x > 0) {
          document.getElementById("container").innerHTML += "<br>";
          x--;
      }
      return result.promise();
  }

  function advance(x) {
      var result = $.Deferred();
      while (x > 0) {
          document.getElementById("container").innerHTML += "&nbsp;";
          x--;
      }
      return result.promise();
  }    

  function blink(y) {
      var result = $.Deferred();
      if (first_time == 1) {
          ctrl = y;
          first_time = 0;
      }
      if (ctrl != 0) {
          if (block == '0') {
              test = document.getElementById("container").innerHTML;
              document.getElementById("container").innerHTML += "\u258B";
              block = 1;
          } else if (block == '1') {
              document.getElementById("container").innerHTML = test;
              block = 0;
          }
          ctrl--;
          setTimeout("blink(y);", 300);
      }
      if (ctrl == 0) {
          first_time = 1;
      }
      return result.promise();
  }

  var sleep = function (ms) {
      var result = $.Deferred();
      setTimeout(result.resolve, ms);
      return result.promise();
  };

  function main() {
      typing('container', 'Subject Name:').then(function () {
          return sleep(500);
      }).then(function () {
          return sleep(500);
      }).then(function () {
          return typing('container', ' Carlos Miguel Fernando');
      }).then(function () {
          return blink('8');
      }).then(function () {
          return sleep(500);
      }).then(function () {
          return typing('container', 'Age: 21');
      }).then(function () {
          return sleep(500);
      }).then(function () {
          return typing('container', 'Location: Somewhere');
      }).then(function () {
          return sleep(500);
      }).then(function () {
          return blink('14');
      }).then(function () {
          return (function () {
              document.getElementById("more").style.display = "block";
          });
      }).then(function () {
          return sleep(300);
      }).then(function () {
          return typing('more', 'Find Out More')
      });
  }

这里有什么问题,有谁知道?我在控制台上没有错误,但只执行了typing('container','Subject Name:')

0 个答案:

没有答案