Pergunta

I'm making an website game which uses many document.getElementById inside a setInterval that runs every 0.25 sec. This makes my webbsite slow after some time. I'm quite new to Javascript but I wonder if there's a more effective or efficient way to call and update a HTML element or create a effective loop that includes game latency and runs every 1 sec?

Foi útil?

Solução

Precache all ID's.

Something in the lines of (pseudo code):

var all_objects = [];
...
onCreateObject = function(){
    all_objects.push(newObject.id)
}

and then just iterate through the all_objects variable.

As for the loop itself, there are solutions out there already: http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/

Outras dicas

set variables outside (above) your setInterval function:

var myDiv = document.getElementById("ID");

and then call them inside the setInterval function:

myDiv.something()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top