Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Way of setting the duration of a particle spawner loop help and way of stopping the function loop

I am using Pixi.js to spawn some particles on a canvas, but I need the particles to be spawning only for some time (3 seconds).

I created a loop function that spawns the particles, but I don't know how to end it from spawning after some time:

var particles = []; 

function spawnFountain(){
    var particle = new PIXI.Sprite.from('./Assets/particle.png');
    app.stage.addChild(particle);
    particles.push(particle);
  
    setTimeout( () => {
        spawnFountain();
    },10)
}

I need this function to stop spawning after 3 seconds.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

var particles = []; 

function spawnFountain(){
    var particle = new PIXI.Sprite.from('./Assets/particle.png');
    app.stage.addChild(particle);
    particles.push(particle);
}

var intervalStart = new Date();
var interval = setInterval(() => {
    var now = new Date();

    if (now - intervalStart > 3000) { // 3 seconds
      clearInterval(interval);
      return;
    }

    spawnFountain();
}, 10)

You need to use a combination of setInterval and clearInterval to start and stop the spawn loop. Keeping track of when the particle generation started and the current time you can determine when the interval should finish.

This is how you can make your code work. However, this approach for generating sprite particles is quite inefficient and will likely end up in performance issues. Consider using a module such as pixi-particles for this matter.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!