简体   繁体   中英

Trouble Using setInterval function

I'm having some trouble getting the below to work. I'm trying to run this asynchronous function called Chat.fetch and I can get the messages it returns to display correctly. When I try to pass it through setInterval it will return the chat messages once but not refresh them every 3 seconds which is what I'm trying to do. Any direction would be awesome. Thanks!

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
   <script src="http://chatbuilder.hackreactor.com/ChatBuilder.js"></script>
  </head>
    <body>
      <script>
         Chat.guide.start();
      </script>


  <h2>Borken Chat</h2>

  <input class="draft" type="text"/> <button class="send" disabled>send</button>



  <ul class="messages">


     PrintChat=function(Chat_Messages){
       var y=Chat_Messages.length;
       for(i=0;i<y;i++){
         Chat.display(Chat_Messages[i]);
       }
     }

   PrintMessages=Chat.fetch(PrintChat);
   setInterval(PrintMessages,3000);


  </script>

</ul>

Try this:

function fetchNew() {
 var PrintChat=function(Chat_Messages){
   var y=Chat_Messages.length;
   for(i=0;i<y;i++){
     Chat.display(Chat_Messages[i]);
   }
 }
 Chat.fetch(PrintChat);
}

setInterval(fetchNew,3000);

The problem in your original code is that PrintMessages is the result of calling a function, rather than function itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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