简体   繁体   English

对附加列表项设置限制

[英]Setting a limit on appended list items

function randomEmoji() {
  var emojis = [
    ':)', ':(', '=D', 'xD', 'o_O', ':|', ':-O', ':$'
  ];
  return emojis[ Math.floor( Math.random() * emojis.length ) ];
}
function textscroll(){
  $('.messages').append("<li>" + randomEmoji() + "</li>");
}
setInterval(textscroll,3000);
function scroll(){
$('.messages').children().hide();
}
setInterval(scroll,5900);

As you can see my code appends a random emoji to .messages(an unordered list in my HTML file) every 3 seconds. 如您所见,我的代码每3秒向.messages(我的HTML文件中的无序列表)附加一个随机表情符号。 How can I limit this to show only the 10 most recent emojis? 如何将其限制为仅显示10个最新表情符号? I've tried to hide the list and show it when it adds a new one. 我试图隐藏该列表,并在添加新列表时显示它。 Not only does it not do this it would also just keep listing them down the page. 它不仅不会这样做,而且还会继续在页面下方列出它们。 I don't know what to do next, this isn't homework or anything actually worthwhile just a weird question thats been driving me crazy the last couple days. 我不知道下一步该怎么做,这不是功课,也不是真正值得一提的奇怪问题,最近几天我一直在疯狂。 Is there anyway besides using .hide() to do this? 除了使用.hide()之外,还有其他方法吗? I feel like using .hide() every 4 seconds is kind of hard on the eyes. 我觉得每隔4秒钟使用.hide()有点困难。 I've searched MDN to find something to implement into my code but have had no luck. 我搜索了MDN,以找到可以在我的代码中实现的功能,但是没有运气。

Right after you append a random Emoji, just check the count of the list items, and if it's above 10 then remove the first one, and you'll be all set. 在您添加随机表情符号后,只需检查列表项的数量,如果它大于10,则删除第一个,就可以了。

function textscroll () {
    $('.messages').append("<li>" + randomEmoji() + "</li>");
    if ( $('.messages li').length > 10 ) {
        $('.messages li').first().remove();
    }
}

Hrm. 人力资源管理。 I think what I would do in this situation is look at the length of the list. 我认为在这种情况下我会做的只是看清单的长度。 If the length of the list is >10. 如果列表的长度> 10。 Just remove the first element. 只需删除第一个元素。 If you want to keep track of all the emojis for some reason, but just display the most recent ten, you could have similar logic, but instead of removing the first element, you could select the first item in the list that doesn't have some custom class...say "last10" that you create, and then append that class to the item when you add an item to the list...then you just display all items that have the class "last10". 如果您出于某种原因想要跟踪所有表情符号,而只显示最近的十个表情符号,则可能具有类似的逻辑,但是除了删除第一个元素外,您还可以选择列表中没有的第一个元素一些自定义类...创建的“ last10”,然后在将项目添加到列表时将该类追加到项目中...然后,您只显示所有具有“ last10”类的项目。

To be more specific: You'd look for an item that doesn't have the class "last10" append the class, then hide the item. 更具体地说:您将寻找一个没有类“ last10”的项目,然后追加该类,然后将其隐藏。

Try something like this ( http://jsfiddle.net/3CBKP/1/ ): 尝试这样的事情( http://jsfiddle.net/3CBKP/1/ ):

Every time you add a new list item check the length to see if it is above the limit: 每次添加新列表项时,请检查其长度是否超过限制:

var messageLimit = 10;

function randomEmoji() {
  var emojis = [
    ':)', ':(', '=D', 'xD', 'o_O', ':|', ':-O', ':$'
  ];

  return emojis[ Math.floor( Math.random() * emojis.length ) ];
}

function textscroll(){
  var $messages = $('.messages');
  var $children = $messages.children();

  $messages.append("<li>" + randomEmoji() + "</li>");

  if($children.length > messageLimit) {
    // there are too many, remove the oldest
    $children.first().remove();
  }
}

setInterval(textscroll, 100);

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

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