0

I have a program which should randomly change the position of a cross in a table every half a second forever, but I can't find a way to make the program wait for half a second without it hanging.

The following is my code, where the sleep(0.5) should be switched with something different:

<!DOCTYPE html>
<html>
<body>
    <table>
        <tr>
            <td id="0x0">X</td>
            <td id="1x0">X</td>
            <td id="2x0">X</td>
        </tr>
        <tr>
            <td id="0x1">X</td>
            <td id="1x1">X</td>
            <td id="2x1">X</td>
        </tr>
        <tr>
            <td id="0x2">X</td>
            <td id="1x2">X</td>
            <td id="2x2">X</td>
        </tr>
    </table>
    <script>
        for(;;) {
            var x = Math.floor(Math.random()*2);
            var y = Math.floor(Math.random()*2);

            id = x.toString() + "x" + y.toString();         
            document.getElementById(id).innerHTML = "O";               
            sleep(0.5);
            document.getElementById(id).innerHTML = "X";
        }
    </script>
</body>
</html>
Beta Decay
  • 805
  • 1
  • 8
  • 20

4 Answers4

3

Javascript don't have function named sleep, but you can use setTiemout to execute a function after given time interval.

I'll also recommend you to use setInterval instead of infinite for loop to execute set of commands after specified time interval.

Another thing is that you're using floor on the random value, which will always be either 0 or 1 and it'll be never 2. Use Math.round to round off the value to the nearest number. So it'll select all the values.

setInterval(function() {
  var x = Math.round(Math.random() * 2);
  var y = Math.round(Math.random() * 2);

  id = x.toString() + "x" + y.toString();
  document.getElementById(id).innerHTML = "O";

  setTimeout(function() {
    document.getElementById(id).innerHTML = "X";
  }, 500);
}, 100);
<table>
  <tr>
    <td id="0x0">X</td>
    <td id="1x0">X</td>
    <td id="2x0">X</td>
  </tr>
  <tr>
    <td id="0x1">X</td>
    <td id="1x1">X</td>
    <td id="2x1">X</td>
  </tr>
  <tr>
    <td id="0x2">X</td>
    <td id="1x2">X</td>
    <td id="2x2">X</td>
  </tr>
</table>
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

Just use setTimeout instead of sleep:

function doSomething () {

    document.getElementById(id).innerHTML = "X";

    var x = Math.floor(Math.random()*2);
    var y = Math.floor(Math.random()*2);

    id = x.toString() + "x" + y.toString();         
    document.getElementById(id).innerHTML = "O";               

    setTimeout(doSomething, 500);

}

Or a setInterval way:

setInterval(function () {

    document.getElementById(id).innerHTML = "X";

    var x = Math.floor(Math.random()*2);
    var y = Math.floor(Math.random()*2);

    id = x.toString() + "x" + y.toString();         
    document.getElementById(id).innerHTML = "O";               

}, 500);
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

could be easier to use setInterval which will call a function for you every x thousandths of a second.

setInterval(function(){
   var x = Math.floor(Math.random()*2);
   var y = Math.floor(Math.random()*2);
   id = x.toString() + "x" + y.toString();         
   document.getElementById(id).innerHTML = "O";               
   document.getElementById(id).innerHTML = "X";
}, 500);
BenG
  • 14,826
  • 5
  • 45
  • 60
  • I think you didn't understand the question, OP want to wait for half second after setting the value and then want to reset the value again please check my answer above. – Tushar Sep 22 '15 at 13:50
0

It looks like you need to use javascript's setInterval function instead. So, if you wanted your code to say, print something out every 10 seconds you'd do something like this:

window.setInterval(printString, 10000);

var printString = function() {
    console.log("hello world");
}

The first argument is the function or operation you wish to perform and the second is the interval (in milliseconds) between each execution.

Sean Morris
  • 135
  • 2
  • 10