daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var custComments = [
  2.     ["Comment 1", "User 1"],
  3.     ["Comment 2", "User 2"],
  4.     ["Comment 3", "User 3"],
  5.     ["Comment 4", "User 4"]
  6. ];
  7.  
  8. var custMsg = document.getElementById("custtest"); // This grabs the element that this code will append the HTML to
  9. var baseMessage = custMsg.children[0].cloneNode(true);
  10. var baseName = custMsg.children[1].cloneNode(true);
  11. var indexNum = 0; // Pulls the current Index of the 2D array custComments
  12. var delay = 3000; // Delay in milliseconds (1000ms per second)
  13.  
  14. function createSafeElement(baseElement, input) {
  15.     // Create a copy of the node, so that our base remains clean.
  16.     var res = baseElement.cloneNode(true);
  17.     res.appendChild(document.createTextNode(input));
  18.     return res;
  19. }
  20.  
  21. function replaceMsg ( comments ) {
  22.     // Because these are customer messages, they are not trustworthy input.  They could contain javascript XSS attacks or similar, so we need to escape them.
  23.     var newMessage = document.createElement('div'); // or 'p' or whatever your "custtest" element actually is
  24.     newMessage.appendChild(createSafeElement(baseMessage, custComments[indexNum][0]));
  25.     newMessage.appendChild(createSafeElement(baseName, custComments[indexNum][1]));
  26.     custMsg = newMessage;
  27.     indexNum += 1; // This part increase the index of the array
  28.     if (indexNum == custComments.length) {
  29.         indexNum = 0;
  30.     } // This if statement will reset the indexNum if it reaches the end of the array
  31. }
  32.  
  33. // setInterval(replaceMsg(custComments),delay); This was actually incorrect
  34. setInterval(function(){replaceMsg(custComments)},delay); // This is the correct syntax
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top