Javascript 我如何做出响应而不保持响应?

Javascript 我如何做出响应而不保持响应?,javascript,Javascript,让我们假设我有一个机器人坐在聊天窗口,它总是抓取页面上最后发送的评论元素的innerHTML,如下所示: setInterval(function() { var comment = document.querySelector('div[name="UserComment"]').innerHTML; if (comment.includes('Hello!')) { document.querySelector('input[aria-label=

让我们假设我有一个机器人坐在聊天窗口,它总是抓取页面上最后发送的评论元素的innerHTML,如下所示:

setInterval(function() {
   var comment = document.querySelector('div[name="UserComment"]').innerHTML;
   if (comment.includes('Hello!')) {
      document.querySelector('input[aria-label="SendComment"]').value = 'Hi!!!!';
      document.getElementById('submitComment').click();
   }
}, 100);

除非它会无限地回答这个问题,否则上面的方法很好。我曾考虑过将注释添加到数组中,并对其进行检查,但某种情况告诉我有一种更优雅的方法。

您可以将已回复的内容存储为
UserComment
元素的属性,并且仅当
UserComment
字段的内容已更改时才回复

setInterval(function() {
   var comment = document.querySelector('div[name="UserComment"]').innerHTML;
   // Store comment as property of the UserComment element
   document.querySelector('div[name="UserComment"]').prop("lastValue", comment);
   // Check for content in comment   and verify that the content is different  to the last comment which has been automatically answered before sending a response
   if (comment.includes('Hello!') && comment != document.querySelector('div[name="UserComment"]').prop("lastValue")) {
      document.querySelector('input[aria-label="SendComment"]').value = 'Hi!!!!';
      document.getElementById('submitComment').click();
   }
}, 100);

我能想到的唯一两种方法是存储过去的响应或每次扫描DOM以查找过去的注释。因此,存储在数组中似乎是最有效的方法。