skip to Main Content

i have an issue with javascript.
var startTime = Date.now(); while ((Date.now() - startTime) < 30000) { display(); }
when i adding these lines of code and trying to run my script,it gives me an error: out of memory.

my original code is:

<script>
   var startTime = Date.now();
while ((Date.now() - startTime) < 30000) {
    display();
}
        function display() {
            setTimeout(showLeft, 1000);
            setTimeout(showRight, 2000);
            setTimeout(showBLeft, 3000);
            setTimeout(showBRight, 4000);
            setTimeout(clearAllDiv, 6000);
        }

        function showLeft() {
            document.getElementById("left").innerHTML = "Hızlı";
        }
        function showRight() {
            document.getElementById("right").innerHTML = "Oku";
        }
        function showBLeft() {
            document.getElementById("bleft").innerHTML = "Çabuk";
        }
        function showBRight() {
            document.getElementById("bright").innerHTML = "Anla";
        }

        function clearAllDiv() {
            document.getElementById("left").innerHTML = " ";
            document.getElementById("right").innerHTML = " ";
            document.getElementById("bleft").innerHTML = " ";
            document.getElementById("bright").innerHTML = " ";
        }
    </script>

i want to repeat my display function for 30 seconds.
when i change 30.000 to 1.000 it works fine,but i need 30 seconds not 1 second.

EDIT:
i was creating infinite loop,and due this reason i got out of memory exception.

2

Answers


  1. Your while loop is blocking the asynchronous execution of the timer callbacks, and in the mean while your loop creates a huge amount of timers, running out of memory.

    Instead you could schedule also the display calls, like this:

    // Execute display 4 times, but only at the right time
    display();
    setTimeout(display, 8000);
    setTimeout(display, 16000);
    setTimeout(display, 24000);
    
    function display() {
        setTimeout(showLeft, 1000);
        setTimeout(showRight, 2000);
        setTimeout(showBLeft, 3000);
        setTimeout(showBRight, 4000);
        setTimeout(clearAllDiv, 6000);
    }
    
    function showLeft() {
        document.getElementById("left").innerHTML = "Hızlı";
    }
    function showRight() {
        document.getElementById("right").innerHTML = "Oku";
    }
    function showBLeft() {
        document.getElementById("bleft").innerHTML = "Çabuk";
    }
    function showBRight() {
        document.getElementById("bright").innerHTML = "Anla";
    }
    
    function clearAllDiv() {
        document.getElementById("left").innerHTML = " ";
        document.getElementById("right").innerHTML = " ";
        document.getElementById("bleft").innerHTML = " ";
        document.getElementById("bright").innerHTML = " ";
    }
    <div id="left"></div>
    <div id="right"></div>
    <div id="bleft"></div>
    <div id="bright"></div>
    Login or Signup to reply.
  2. you have created an infinite loop. maybe something more like this:

    var startTime = Date.now();
    checkTimer(startTime);
    
    function checkTimer(startTime) {
        if(((Date.now() - startTime) < 30000)) {
          display();
          setTimeout(() => {
            checkTimer(startTime)
          }, 1000);
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search