سؤال

I ripped a nice trick from somewhere else on this site that allows you to delay the cycles of a loop. I modified it a little for my own use with a Javascript bot that runs on TamperMonkey, in Chrome Version 34.0.1847.131 m.

For whatever reason, as soon as the loop is called, a MASSIVE memory leak begins to occur (at about 40,000K - 80,000K per sec) and it doesn't loop at all. My computer already crashed twice because I was neither prepared for nor expecting it.

The Function

 //Tests if Bot is still connected
var connected = true;

function testConnection() {    //  create a loop function
   setTimeout(function () {    //  call a 3s setTimeout when the loop is called
                                //  your code here
      randQuery = "PING " + Math.floor((Math.random()*8234));  //Creates a value of "PING " plus a random number attatched to the end to ensure that window.find only matches with the new ping 
    
      document.getElementById('msg').value = randQuery; //Inputs the above-created value into the message box
      DoMessage();                                     //Submits what's in the message box. Fails if m.xat.com no longer has a connection to the chatroom.
      var connected = window.find(randQuery);  //If the above failed, randQuery will not be found, returning the value of "False". There's probably a better solution for this?
   
        if (connected == true) {
            sendMessage("succeeded"); //DEBUG
        }
        else {
            sendMessage("failed");  //DEBUG
            connected = false;
            Reb00t();              //Reloads the page if DoMessage() failed
        }
       
       
      while (connected == true) {    //  if bot is still connected, call the loop function
         testConnection();          //  ..  again which will trigger another 
      }                            //  ..  setTimeout()
   }, 9000)
}

setTimeout( function startPingLoop() {
    testConnection();                      //  start the loop
}
,1200);

Is there something I screwed up here? I've never dealt with a memory leak before.

Full Code

هل كانت مفيدة؟

المحلول

As long as connected is equal to true, the while loop will keep firing over and over, with no delay in between. A while loop is not asynchronous - it will pause everything else going on in the script until it is over.

Think of a loop like an automatic code writer. It writes a function that goes for as long as until the statement in the brackets is false.

  while (connected == true) {
     testConnection();
  }

is equivalent of writing:

  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
  if (connected == true) {
     testConnection();
  }
...

over and over again for as long as connected is equal to true. So if connected stays equal to true, the while loop tries to write an infinitely long code, causing an infinite loop until the testConnection() function makes connected equal to false. If your computer had infinite memory, this infinitely long code would take up all of it - but your computer doesn't have an infinite supply of memory, and so it crashes.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top