2

I've tried Math.random But then it says it is undefined.

function startBlinking() {
    setInterval(function() {
        blink();
    }, 8000);
}

function blink() {
    goggleY = goggleY - 25;
    setTimeout(function() {
        if (goggleY > goggleY - 25) {
            goggleY = goggleY + 25
        }
    }, 150);
}

is there a way I can make the function call randomly within set numbers? (EX: random(1, 10))? any help would be appreciated :)

flaxel
  • 4,173
  • 4
  • 17
  • 30

2 Answers2

0
function blink() {
  goggleY = goggleY - 25;
  setTimeout(function Math.random() {
    if (goggleY > goggleY - 24) {
      goggleY = goggleY + 25
    }
  }, 150);
}
startBlinking();

Heres the code with Math.random
0

Taken from setTimeout() - in for loop with random delay

    setTimeout(function () {
        console.log(i);
    }, Math.floor(Math.random() * 1000)

Is what you are looking for. So in your case

//Function to generate random int from min to max inclusive on both ends
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}    

function blink() {
  goggleY = goggleY - 25;
  setTimeout(function() {
   if (goggleY > goggleY - 24) {
    goggleY = goggleY + 25
   }
  }, getRandomInt(1,10));
}
blink();

The important part for you to randomize a setTimeout is in the last bit after the comma.

setTimeout(function(){ alert("Hello"); }, <'the part where we set how long to wait in milliseconds'>);

Good luck and happy hacking.

FujiRoyale
  • 762
  • 10
  • 26