我想要生成一个随机数,我在条件语句(If...else)中使用它作为变量。条件语句发生的function PositionLoop()具有赋值requestAnimationFrame。但是,我希望随机数不要在每一帧中重新生成.这太频繁了,太快了。例如,我希望这个数字每3秒钟改变一次。另一个问题是条件语句包含一个变量(Font),我在代码 in 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();
…发布于 2017-09-13 14:44:08
您可以使用一个单独的间隔:
(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();
})();https://stackoverflow.com/questions/46200077
复制相似问题