具有不同帧率的函数中的随机数
Random Number in function with different frameRate
我想生成一个随机数,我在条件语句 (If...else
) 中将其用作变量。
function PositionLoop()
,其中条件语句发生,具有赋值 requestAnimationFrame
。但是,我希望随机数不要在每一帧中重新生成。这太频繁也太快了。例如,我希望数字每 3 秒更改一次。另一个问题是条件语句包含一个变量(Font
),我在代码的另一行再次使用它 inside function PositionLoop()
…
我已经尝试过不同的东西——首先我为随机数创建了一个函数,并在另一个函数中调用变量 function PositionLoop()
(Accessing variables from other functions without using global variables),然后我尝试了全局变量 – ,但是这是行不通的。有人可以帮我吗? – 非常感谢!
这是我的代码结构:
…
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();
…
您可以为此使用一个单独的时间间隔:
(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();
})();
我想生成一个随机数,我在条件语句 (If...else
) 中将其用作变量。
function PositionLoop()
,其中条件语句发生,具有赋值 requestAnimationFrame
。但是,我希望随机数不要在每一帧中重新生成。这太频繁也太快了。例如,我希望数字每 3 秒更改一次。另一个问题是条件语句包含一个变量(Font
),我在代码的另一行再次使用它 inside function PositionLoop()
…
我已经尝试过不同的东西——首先我为随机数创建了一个函数,并在另一个函数中调用变量 function PositionLoop()
(Accessing variables from other functions without using global variables),然后我尝试了全局变量 – ,但是这是行不通的。有人可以帮我吗? – 非常感谢!
这是我的代码结构:
…
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();
…
您可以为此使用一个单独的时间间隔:
(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();
})();