javascript 定时数组

发布于 2024-11-25 02:42:47 字数 288 浏览 2 评论 0原文

我错过了一些小事情..打印数组但不在行之间等待。

<script type="text/javascript">

function showLines()
{
arr =
[
  "Hi!",
  "Welcome!",
  "Hello!"
]

var duration=2000;

document.getElementById("quotes").innerHTML=arr;
setTimeout('showLines()',duration);

}
</script>

I'm missing some little thing.. prints the array but doesn't wait in between lines.

<script type="text/javascript">

function showLines()
{
arr =
[
  "Hi!",
  "Welcome!",
  "Hello!"
]

var duration=2000;

document.getElementById("quotes").innerHTML=arr;
setTimeout('showLines()',duration);

}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执笔绘流年 2024-12-02 02:42:47

那是因为你只是打印出整个数组,试试这个。

    function showLines(_index) {
       var arr =["Hi!", "Welcome!", "Hello!"], html = '', i, index = _index || 0,
       newIndex;

       for (i = 0; i < index && i < arr.length; ++i) {
          html += arr[i] + "<br />";
       }
       document.getElementById("quotes").innerHTML=html;

       newIndex = index + 1;
       if (newIndex < arr.length) {
          setTimeout(function() {showLines(newIndex);}, 2000);
       }
    }

那应该可以解决问题。

如果您一次只想要一个,则替换

           for (i = 0; i < index && i < arr.length; ++i) {
              html += arr[i] + "<br />";
           }

document.getElementById("quotes").innerHTML=arr[index];

That's because your just printing out the whole array, try this.

    function showLines(_index) {
       var arr =["Hi!", "Welcome!", "Hello!"], html = '', i, index = _index || 0,
       newIndex;

       for (i = 0; i < index && i < arr.length; ++i) {
          html += arr[i] + "<br />";
       }
       document.getElementById("quotes").innerHTML=html;

       newIndex = index + 1;
       if (newIndex < arr.length) {
          setTimeout(function() {showLines(newIndex);}, 2000);
       }
    }

That should do the trick.

If you only want one at a time then replace

           for (i = 0; i < index && i < arr.length; ++i) {
              html += arr[i] + "<br />";
           }

with

document.getElementById("quotes").innerHTML=arr[index];
以歌曲疗慰 2024-12-02 02:42:47

该行将

document.getElementById("quotes").innerHTML=arr;

通过用逗号连接将 arr 转换为 String。因此,你会看到

嗨!,欢迎!,你好!

此函数是幂等,这可能不是您想要的。我认为您缺少的是一个索引,它可以让您知道下次执行该函数时您位于数组的哪个元素,并将 quotes 元素的内容替换为中的下一项数组。

The line

document.getElementById("quotes").innerHTML=arr;

will convert arr into a String by joining it with commas. Therefore, you will see

Hi!, Welcome!, Hello!

This function is idempotent, which is probably not what you are going for. I think what you're missing is an index that lets you know which element of the array you are on the next time the function is executed, and replaces the content of the quotes element with the next item in the array.

一张白纸 2024-12-02 02:42:47

这里的大多数答案都是在每次迭代时重新初始化数组。这样做是没有意义的。你应该这样做:

<script type="text/javascript">

function showLines(){

    var arr =
    [
      "Hi!",
      "Welcome!",
      "Hello!"
    ], i = 0;

    (function showLinesHelper(){
        document.getElementById("quotes").innerHTML += arr[i++]+'<br />';
        if(i < arr.length)
            setTimeout(showLinesHelper, 2000);
    })();

}

</script>

这样它就可以工作,你的数组和 i 只初始化一次。

编辑回应评论:

<script type="text/javascript">

function showLines(){

    var arr =
    [["Hi!", 3000],
     ["Welcome!", 500],
     ["Hello!", 1000]]
    , i = 0;

    function showLinesHelper(){
        document.getElementById("quotes").innerHTML += arr[i++][0]+'<br />';
        if(i < arr.length)
            setTimeout(showLinesHelper, arr[i][1]);
    }

    setTimeout(showLinesHelper, arr[0][1]);
}

</script>

Most answers here are re initializing your array on each iteration.It makes no sense to do that. You should do it like this:

<script type="text/javascript">

function showLines(){

    var arr =
    [
      "Hi!",
      "Welcome!",
      "Hello!"
    ], i = 0;

    (function showLinesHelper(){
        document.getElementById("quotes").innerHTML += arr[i++]+'<br />';
        if(i < arr.length)
            setTimeout(showLinesHelper, 2000);
    })();

}

</script>

This way it works, and your array, and i are only initialized once.

EDIT In response to comment:

<script type="text/javascript">

function showLines(){

    var arr =
    [["Hi!", 3000],
     ["Welcome!", 500],
     ["Hello!", 1000]]
    , i = 0;

    function showLinesHelper(){
        document.getElementById("quotes").innerHTML += arr[i++][0]+'<br />';
        if(i < arr.length)
            setTimeout(showLinesHelper, arr[i][1]);
    }

    setTimeout(showLinesHelper, arr[0][1]);
}

</script>
初雪 2024-12-02 02:42:47

你从来没有要求他等待。您只是每 2 秒调用一次相同的函数。
尝试使用 showLines(i)、innerHTML += arr[i] 和 setTimeout(showLines,duration,i++)

<script type="text/javascript">

function showLines(i)
{
arr =
[
  "Hi!",
  "Welcome!",
  "Hello!"
]

var duration=2000;

document.getElementById("quotes").innerHTML += arr[i];
i++;
setTimeout(showLines,duration,i);

}
</script>

You never asked him to wait. You're just calling the same function every 2 seconds.
Try with showLines(i), innerHTML += arr[i], and setTimeout(showLines,duration,i++)

<script type="text/javascript">

function showLines(i)
{
arr =
[
  "Hi!",
  "Welcome!",
  "Hello!"
]

var duration=2000;

document.getElementById("quotes").innerHTML += arr[i];
i++;
setTimeout(showLines,duration,i);

}
</script>
野の 2024-12-02 02:42:47

首先,您应该将代码包装在 onloaddomready 函数中。 jQuery 擅长于此。您应该使用 window.onload = myfunc; 来执行此操作。

您的代码应如下所示:

<script type="text/javascript">
  var init = function () {
    var myarray = ["Hi!","Welcome!","Hello!"], index = 0, printline = function () {
      document.getElementById("quotes").innerHTML += myarray[index];

      if (index + 1 < myarray.length) {
        setTimeout(printline, 2000);
      }  
      index++;
    };
    printline();
  }
  window.onload = init;

</script>

First of all, you should wrap your code in an onload or domready function. jQuery is good at this. You should use window.onload = myfunc; to do this.

Your code should look like this:

<script type="text/javascript">
  var init = function () {
    var myarray = ["Hi!","Welcome!","Hello!"], index = 0, printline = function () {
      document.getElementById("quotes").innerHTML += myarray[index];

      if (index + 1 < myarray.length) {
        setTimeout(printline, 2000);
      }  
      index++;
    };
    printline();
  }
  window.onload = init;

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文