0

I am currently attempting to extract and update global variables from inside of an requestAnimationFrame() loop. The global variables inside of this scope does not update and am searching for a way to update the variables.

Here is a sample of my code (full source can be found at clmtrackr-clm_Video):

Var _myGlobal = "one";
function drawLoop() {
    requestAnimFrame(drawLoop);
    overlayCC.clearRect(0, 0, 400, 300);
    //psrElement.innerHTML = "score :" + ctrack.getScore().toFixed(4);
    if (ctrack.getCurrentPosition()) {
        ctrack.draw(overlay);
        _myGlobal = "two";
        document.getElementById('tester').innerHTML = _myGlobal; //prints "two"
    }
}
document.getElementById('tester').innerHTML = _myGlobal; //prints "one"

Note: I am trying to pass out the values while the video is still running (animationframe).

Any help would be much appreciated.

Thanks.

soktinpk
  • 3,778
  • 2
  • 22
  • 33
The_Doctor
  • 181
  • 5
  • 16
  • You don't assign to any variables in that code snippet, so what would you expect to update? – Bergi Jul 21 '14 at 11:14
  • @Bergi, my apologies i did not include because the code didnt work, i have up=dated the code to better match my current problem/question. – The_Doctor Jul 21 '14 at 22:54
  • The statement that prints `one` seems to be executed before `drawLoop` is called, where the other statement successfully updates the variable and prints `two`? – Bergi Jul 21 '14 at 22:58
  • @Bergi yes, this seems to be the case, i just confirmed it. Any solutions? – The_Doctor Jul 22 '14 at 00:25
  • I don't know what you *want* to do? – Bergi Jul 22 '14 at 00:29
  • be able to update the global variable and use its value outside of the loop (or will that not work, and should I throw all of my code inside of the `animationloop`?) – The_Doctor Jul 22 '14 at 00:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57719/discussion-between-the-doctor-and-bergi). – The_Doctor Jul 22 '14 at 00:38
  • How are you calling the `animationloop`? When will it be finished? I guess you use `requestAnimationFrame`, which is *asynchronous*, so throwing everything that depends on the future values in the function is the only solution. – Bergi Jul 22 '14 at 00:40
  • Thanks, expecting that, but hoping there would have been another way. Cheers – The_Doctor Jul 22 '14 at 00:42

0 Answers0