简体   繁体   English

jQuery通过遍历数组来更改html文本

[英]jQuery change html text by iterating over array

If I write the html: 如果我写的HTML:

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<h1 id="message">
</h1>

and the JS: 和JS:

messages = ["Here", "are", "some", "messages."]

$(function() {

  for (var i = 0; i < messages.length; i++) {
    $('#message').html(messages[i]).delay(1000);
  }

});

and load the page, I expect to see each string in the array show up with a delay in between. 并加载页面,我希望看到数组中的每个字符串之间都出现延迟。 However, all I see is "messages." 但是,我所看到的只是“消息”。 appear. 出现。 It seems that the for loop iterates immediately through each value in the array before performing any delay. 似乎for循环会在执行任何延迟之前立即遍历数组中的每个值。

I have seen another method for getting the desired visual result ( How can I change text after time using jQuery? ), but I would like to know why the earlier method does not work. 我已经看到了获得所需视觉效果的另一种方法( 如何使用jQuery在一段时间后更改文本? ),但是我想知道为什么早期的方法不起作用。 What is going on when this code is executed? 执行此代码后会发生什么情况?

This is how i would delay my message changing. 这就是我会延迟更改消息的方式。

 function delayLoop(delay, messages) { var time = 100; $(messages).each(function(k, $this) { setTimeout(function() { $("#message").html($this); }, time) time += delay; }); } delayLoop(1000, ["Here", "are", "some", "messages."]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="message"> </div> 

All I did was for each message delay by an additional delay time. 我所做的就是为每个消息延迟一个额外的延迟时间。 It works in async mode so its not ui blocking and the messages will display a second after one another. 它以异步模式工作,因此不会被ui阻塞,并且消息将一秒钟接一秒显示。

EDIT: 编辑:

Removed the .delay from the .html it is redundant. 从.html中删除.delay是多余的。

Note that jQuery's delay is specifically for effects; 请注意,jQuery的delay专门用于效果。 methods like html do not use the effects queue and are therefore not affected by delay . 诸如html方法不使用效果队列,因此不受delay影响。

This is a problem better solved with JavaScript's native setTimeout function. 使用JavaScript的本机setTimeout函数可以更好地解决此问题。 There are many ways to do this; 有很多方法可以做到这一点。 in fact, you don't even need jQuery! 实际上,您甚至不需要jQuery!

 let messages = ["Here", "are", "some", "messages."]; let delay = 1000; let header = document.getElementById("message"); messages.forEach(function(message, i) { setTimeout(function() { header.innerText = message; }, delay * i); }); 
 <h1 id="message" /> 

You would need something along the lines of 您将需要一些类似的东西

$(function() {
  for (var i = 0; i < messages.length) {
    var done=false;
    $('#message').html(messages[i]).delay(1000).queue(function(){
     done=true;
     $(this).dequeue();
    });
     if(done==true){ 
      i++;
     }
   }
});

Thank you for the answers and comments--very helpful. 感谢您的回答和评论-非常有帮助。

I also found this post helpful: Node.js synchronous loop , and from it wrote this (which also works): 我还发现这篇文章很有帮助: Node.js同步循环 ,并从中写下了这一点(也可以):

function changeText() {
    var msg = messages.shift();
  $('#message').html(msg).show(0).delay(1000).hide(0, function() {
    if (messages.length > 0) {
        changeText();
    }
  });
}

(I used .show and .hide because without them only one of the array values appeared. I'm not sure why that is, but that's a question for another time.) (我使用了.show和.hide,因为没有它们,仅会出现一个数组值。我不确定为什么会这样,但这是另一个问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM