3

I wish to create a speed reading software of my own. For that, I need the text to continuously change and stop when the passage gets over. I have tried something where the contents are included in any array and the text continuously changes perfectly but it doesnt stop and continuosly rotates the array contents. I wish to stop the text when the array comes to an end. PLease help me !!!!

And here is my code,

<html>
<head>
  <title>Replacing Text</title>
  <script language="JavaScript">
    var msgIX = 0
    var msgs = new Array(
      "Notice anything different?",
      "The text you are looking at has changed.",
      "This is a handy way of sending messages to your users."
    )

    function scrollMessages(milliseconds) {
      window.setInterval("displayMessage()", milliseconds)
    }
    function displayMessage() {
      if(document.getElementById != null) {
      var heading = document.getElementById("scrollme")
      heading.firstChild.nodeValue = msgs[msgIX]
    }else{
      if(navigator.appName == "Microsoft Internet Explorer") {
        var heading = document.all.item("scrollme")
        heading.innerText = msgs[msgIX]
      }
    }
    ++msgIX
    msgIX %= msgs.length
   }
   </script>
</head>
<body onload="scrollMessages(2000)">
  <h1 align="center" id="scrollme">Watch this text very carefully!</h1>
</body>
</html>
lozadaOmr
  • 2,565
  • 5
  • 44
  • 58
user3264821
  • 175
  • 2
  • 18
  • Store the returnvalue of `setInterval`, and use that to stop the interval-function (using `clearInterval()`) when you've reached the last message (`msqIX == msgs.length-1`). – towr Feb 03 '14 at 07:14

1 Answers1

1

Try this

<html>
<head>
    <title>Replacing Text</title>
    <script language="JavaScript">
        var msgIX = 0
        var msgs = new Array(
                "Notice anything different?",
                "The text you are looking at has changed.",
                "This is a handy way of sending messages to your users."
        )

        function displayMessage(milliseconds) {
            if(msgIX < msgs.length){
            if(document.getElementById != null) {
                var heading = document.getElementById("scrollme")
                heading.firstChild.nodeValue = msgs[msgIX]
            }else{
                if(navigator.appName == "Microsoft Internet Explorer") {
                    var heading = document.all.item("scrollme")
                    heading.innerText = msgs[msgIX]
                }
            }
            ++msgIX;
            window.setTimeout(function(){displayMessage(milliseconds);},milliseconds);

        }
        }
    </script>
</head>
<body onload=" window.setTimeout(function(){displayMessage(5000);},5000);">
<h1 align="center" id="scrollme">Watch this text very carefully!</h1>
</body>
</html>
pawinder gupta
  • 1,225
  • 16
  • 35