如何阻止外部函数调用但允许内部函数

时间:2020-06-18 06:16:47

标签: javascript

是否可以阻止调用来自外部函数的函数,但允许其根据需要进行内部重复?因此,每次只能在屏幕上出现一种粒子效果:)

编辑。如果我现在运行它,该粒子将生成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;
    }
    

0 个答案:

没有答案
相关问题