1

I would like to generate a random number, which I use in a conditional statement (If...else) as a variable. function PositionLoop(), in which the conditional statement takes place, has the assignment requestAnimationFrame. However, I would like the random number not to re-generate in each frame. This is way too often and too fast. I would like the number to change every 3rd sec for example. Another problem is that conditional statement contains a variable (Font), which I am using again at another line in the code inside function PositionLoop()

I have already tried different things – first I created a function for the random number and call the variable inside the other function function PositionLoop() (Accessing variables from other functions without using global variables), then I tried global variables – , but it does not work. Can somebody help me with it? – Thank you very much!

This is my code-structure:

…

function positionLoop() {
    requestAnimationFrame(positionLoop);

    …


    var Zufallszahl1 = random(0,30);
    var Font;
    if (Zufallszahl1 = 6) {
        Font = …;
    } else if (Zufallszahl1 = 8) { 
        Font = …;
    } else {
        Font = …;
    };

    if (parameter < x) {
        Schriftart = …;
    } else if (parameter > x) { 
        Schriftart = Font;
    } else {
        Schriftart = …;
    };

    var Gestalt = selectAll('.class1');
    for (var i = 0; i < Gestalt.length; i++) {
        Gestalt[i].style('font-family', Schriftart);
        Gestalt[i].style(…);
        Gestalt[i].style(…);
        …
    };

    …

}positionLoop();

…
S.Hamburg
  • 13
  • 4
  • 1
    _"However, I would like the random number not to re-generate in each frame. This is way too often and too fast."_ Well, how often _do_ you want it to update? – James Thorpe Sep 13 '17 at 14:30
  • 1
    *"an if loop"* : what is that? – trincot Sep 13 '17 at 14:37
  • Agreed with both previous comments. Just to help you a bit, `positionLoop` will receive an HighResTimeStamp (similar to performance.now) that you can use to trigger special events at intervals. – Kaiido Sep 13 '17 at 14:39

1 Answers1

0

You could use a separate interval for that:

(function () {
    var Zufallszahl1;
    function changeZufallszahl1() {
        Zufallszahl1 = random(0,30);
        if (Zufallszahl1 = 6) {
            Font = …;
        } else if (Zufallszahl1 = 8) { 
            …
        } else {
            …
        }

        …

    }

    changeZufallszahl1();
    // Repeat with whatever delay you want between changes
    setInterval(changeZufallszahl1, 1000); 

    // Keep your animation loop separate:
    function positionLoop() {
        requestAnimationFrame(positionLoop);

        …


    }
    positionLoop();
})();
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thanks for your answer! I really forgot to mention sth. impotant (sorry): the conditional statement (if...else) contains a variable (Font), which I am using again at another line in the code inside function PositionLoop()… – S.Hamburg Sep 13 '17 at 15:48
  • Just define that variable at the same place as `Zufallszahl1`, so within the IIFE scope. – trincot Sep 13 '17 at 16:15