1

I'm attempting to build an app in Processing.js that rotates an array of objects every 15 min (seconds for now). My idea was to just increment the index every time the second hand strikes 0/15/30/45, but since it's inside a draw loop it executes +1 times every frame in that second, setting my index to like, 30. How can I ensure only one increment gets through?

var i = 0;
var draw = function() {
    background(148, 221, 255);
    var s = second();
    var m = minute();
    var h = hour();
    text(h + " : " + m + " : " + s, 5, 395);

    var Rotation = function() {
        if (s === 0 || s === 15 || s === 30 || s === 45) {
            i+= 1;
            Array[i];
        }
    };
    Rotation();
    text(i,50,50);
};

Thanks

dpren
  • 1,225
  • 12
  • 18
  • 1
    http://stackoverflow.com/questions/10912795/processing-js-timer – Qaz Jun 25 '14 at 20:51
  • Could you try using setInterval()? – Josh Jun 25 '14 at 20:59
  • FPS time accuracy is not my problem. My problem is not knowing how to limit the number of function executions inside the scope of my draw loop. – dpren Jun 25 '14 at 21:00
  • Actually, forget that. Use the link Qaz posted. You need to compare how much time has passed like he said, rather than using s to check in intervals of 15 – Josh Jun 25 '14 at 21:04
  • Why does `Rotation` exist? It is only defined within that scope when the `draw` is executed, and isn't passed anywhere. – Kevin B Jun 25 '14 at 21:07
  • The mechanism I'm looking for is akin to a ratchet, something that clicks once and only once until the next condition despite the draw loop executing the function over and over again. – dpren Jun 25 '14 at 21:07

1 Answers1

3

Add a variable that checks if the current inverval (0,15,30,45) has already been used / set.

var i = 0;
var last_interval;
var draw = function() {
    background(148, 221, 255);
    var s = second();
    var m = minute();
    var h = hour();
    text(h + " : " + m + " : " + s, 5, 395);

    var Rotation = function() {
        if (s === 0 || s === 15 || s === 30 || s === 45) {
            if(last_interval === undefined || last_interval != s){
                last_interval = s;
                i+= 1;
                Array[i];
            }
        }
    };
    Rotation();
    text(i,50,50);
};
Gabs00
  • 1,869
  • 1
  • 13
  • 12