Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:48:25+00:00 2026-06-16T05:48:25+00:00

I need to queue snippets and execute them all at a given time (Once

  • 0

I need to queue snippets and execute them all at a given time (Once a second). The snippets are actually player actions like jump(), attack(), walk(‘left’) etc.

When a user presses a key I need to queue his action and execute all actions once a second.

Right now my approach, for lack of a better idea is to add each snippet to an array and loop through it with eval(). This is my code:

var queue = [];

// On player or AI action
queue.push('attack()'); // Could be walk('left'), jump() etc.

// On new frame
for(var i=0;i<queue.length;i++){
  eval(queue[i]);
  }
queue = [];

I’m sure my approach is terrible but I wanted to clarify what I want to do, I suppose a problem such as this is not so uncommon. Any info/ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:48:26+00:00Added an answer on June 16, 2026 at 5:48 am

    It is terrible – eval is evil. Instead, use closures:

    var queue = [];
    
    queue.push(function() { attack(); });
    
    while (queue.length) {
        queue.shift().call();
    }
    

    This is a general case, but it can in this case be simplified: function() { attack(); } is a more complicated and slower way to write attack, so queue.push(attack) would work just as well (if you have a a function attack() { ... } defined somewhere accessible). You can’t do that, obviously, if your function takes parameters (or rather, if each function takes different parameters; you can supply unified parameter list in the call(thisobj, param) call).

    EDIT for your additional query:

    In the code that you wrote, this should have captured the variable into the closure; if you change the variable, that value will be what you’ll get when you execute it. If you got undefined, I guess you executed closerEnemy = undefined somewhere later. This is a common source of errors when, for example, people try to bind a click handler to several elements in a loop, and use the loop counter in the handler – the handler captures the counter and not its value, and will always later evaluate to the number of elements (the last value it was left with when the loop finished).

    To capture the value and not the variable, use this trick:

    (function(capturedCloserEnemy) {
      queue.push(function() {
        attack(capturedCloserEnemy);
      });
    })(closerEnemy);
    

    (The variables are named differently just for readability; they could all be named the same and it would not matter, because shadowing.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to set up a queue system using some SQL tables, like the
I need a queue on n items where insertion of ( n +1) th
I need for a queue which can be accessed simultaneously by 2 threads, one
I need a priority queue where I can increase or decrease the priority key.
I need to know when a Queue is closed and wont have more items
I need to set up a job/message queue with the option to set a
I need some kind of priority queue to store pairs <key, value> . Values
I need to implement a FIFO queue for messages on a game server so
Consider a queue holding a lot of jobs that need processing. Limitation of queue
I need to send some information on a VxWorks message queue. The information to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.