Home Random Number in function with different frameRate
Reply: 1

Random Number in function with different frameRate

S.Hamburg
1#
S.Hamburg Published in 2017-09-13 14:23:50Z

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();

…
trincot
2#
trincot Reply to 2017-09-13 14:44:08Z

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();
})();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300979 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO