是否可以阻止调用来自外部函数的函数,但允许其根据需要进行内部重复?因此,每次只能在屏幕上出现一种粒子效果:)
编辑。如果我现在运行它,该粒子将生成1次,因为if(!is_playing)会阻止它
var timer = 0;
var is_playing = false;
function PARTICLE8_AREA (sender, repeat, speed, rangeleft, rangetop, width, height, bgcolor) {
if (sender && !is_playing) {
is_playing = true;
if (timer <= repeat) {
setTimeout(function() {
timer++;
var randleft = rand(-rangeleft, rangeleft);
var randtop = rand(-rangetop, rangetop);
function rand(min, max) {
return Math.random() * (max - min) + min;
}
var particle = document.createElement('div');
particle.style.backgroundColor = bgcolor;
particle.style.left = parseInt(randleft);
particle.style.top = parseInt(randtop);
document.documentElement.style.setProperty('--width', width + 'px');
document.documentElement.style.setProperty('--height', height + 'px');
particle.classList.add('particle');
sender.appendChild(particle);
setTimeout(function() {
particle.remove();
}, 2999);
PARTICLE8_AREA(sender, repeat, speed, rangeleft, rangetop, width, height, bgcolor);
}, speed);
} else if (timer >= repeat) {
timer = 0;
is_playing = false;
}
}
return is_playing;
}