简体   繁体   中英

How to handle multiple events at the same time?

I'm creating a little game in JS where a lot of events occurs and often at the same time (like modify html elements or globals js variables..)

To handle this, i'm using a "setInterval" wich loop every 1ms to look in a table if there is something to do ("requests")

I populate the table every time i need to change something on the page, but sometimes i need change several differents things at the same time.

The problem is that it does not update at the same time, it's delayed when there is like 2,3 or more requests at the same time. I would like this to be instant and if there is 2 requests or more to do at the exact same time, it should do it at the same time and not delaying each requests (example of requests : updating the width of a div in jquery).

Thanks for the help..

var queue = {};

var fixedUpdate = setInterval(function() { //do all the requests in queue 

    $.each(queue, function(key, val) { //for each request present in the queue

        //do something (requests are about updating display or some global variables)

        /*...*/

        //after completing the request we delete it from the queue
        delete queue[key];
    });

}, 1);

Polling a queue is not an appropriate way to write Javascript. Because of the single threaded nature of Javascript in the browser, you will not get good responsiveness in pretty much any of your app if you are continuously polling a queue on a short interval.

Javascript is built from the ground up to be an event driven language and it works effectively when it is used in an event driven fashion.

You must have events that are putting things in the queue. You should trigger processing of the queue WHEN something is added to the queue, NOT have some separate timer that polls the queue. Trigger the processing of the queue WHEN things are added to the queue will also give you immediate response rather than having a timer delay.

And, further whenever you finish processing an event in the queue, your code should check to see if there is something else in the queue and process that too. This way, you get all queued items processed as close to when they were added to the queue as possible in a single threaded environment.

Further, the main browser Javascript is single threaded so you will only ever be processing one queued item at a time. You cannot process multiple requests at the exact same instant. The browser does have real threads with webWorkers, but those cannot access the DOM so are probably not relevant here.

Further still, per the HTML5 specification, the minimum time for setInterval() is 5ms and that is only if there is not other code running at the time.

刚刚发现了造成延迟的原因...。我有一个函数正在两个值(最小最大值)之间创建一个随机重复时间,但是我在数学运算中失败了,当您给它一个函数时,它甚至返回一个随机值两个相同的最小值和最大值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM