0

So i just wanted to make a generator that generates a random value every 10 seconds. And instead it generates a random value every frame after 10 seconds of waiting. Please correct me.

var imgs = [];
var a = 0

function randomizea() {
   a = int(random(5));
}

function setup() {
  createCanvas(1400, 850);

//  for (var i=0; i<5; i++) {
//    imgs[i] = loadImage("data/img"+i+".png");
//  }
}
function draw() {
  background(150, 100, 150);
  setInterval(randomizea, 1000);
//  image(imgs[a], 0, 0);
text(a, 0, 50);
}
talonmies
  • 70,661
  • 34
  • 192
  • 269

1 Answers1

1

You should generally not use setInterval() with P5.js. Instead, rely on the timing mechanisms that P5.js already gives you.

Here's a simple example that would print something to the console every 10 seconds:

function draw(){
    if(frameCount % 600 == 0){
        console.log("here");
    }
}

Since 60 frames fire per second, then 600 frames is 10 seconds. We use the % modulus operator to check that the frameCount variable is a multiple of 600, which means that we're at a multiple of 10 seconds.

You could also use the millis() function and check whether a certain time has elapsed.

Related posts:

Please also consult the P5.js reference for more information.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Yeah... tried that already but there seems to be a problem about my understanding of the random() function because even with your suggestion I got the same results :/ – Protegit Jan 16 '18 at 17:58
  • What exact results do you get? – Kevin Workman Jan 16 '18 at 18:18
  • When i display 'var a' as text on canvas, it says it equals to 0. Then, after a second it starts rapidly and randomly changing its value every frame until I stop the program. – Protegit Jan 16 '18 at 18:55
  • Note that you're calling `setInterval()` inside the `draw()` function, which means you're setting up 60 timers every single second. In two seconds you'll have 120 timers. In 10 seconds you'll have 600 timers. Please try the code I posted in my answer. – Kevin Workman Jan 16 '18 at 19:08
  • When using your code: Uncaught TypeError: console.println is not a function. I just want a random number every 10 seconds and I want that number to stay as it is for the next 10 seconds and not change to "undefined" or "null" or anything else. Then after 10 seconds I want that number to change to a different (or the same) random number. – Protegit Jan 16 '18 at 19:34
  • @Protegit Sorry, that was a type. It should say `console.log()` instead. But note that my code is just an example of how to do something every 10 seconds. Please also try to understand my latest comment, where I explained why `setInterval()` isn't working. – Kevin Workman Jan 16 '18 at 21:50
  • Yes, yes. I understand your code and I understand why my code isnt working. What I dont understand is how to fix it, because even with your code I could do it. Trying to use the modulus was actually my first attempt which did not succeed. Then I implemented setInterval() which wasnt working either... so in order to fix it I will probably have to get a better understanding of the random() fuction and/or specific function/variable placement (so that they are not out of scope). – Protegit Jan 17 '18 at 16:58
  • 1
    @Protegit Use the code I gave you, but print out a random number instead of the message `"here"`. Work your way up from there. If you get stuck, post a [mcve] in a new question post. Good luck. – Kevin Workman Jan 17 '18 at 17:08