T
T
TempUserMain2014-05-25 21:02:39
JavaScript
TempUserMain, 2014-05-25 21:02:39

console.log() synchronization

There is a code:
(In the loop, the xml file is read, parsed, information is obtained from certain blocks, and then, using RegExp, the search for certain sentences)

var tempNumber = 0;

for (i=0; i < 3; i++) {

    <...>
    tempNumber++;
    <...>


}

console.log("tempNumber: " + tempNumber);

As a result, the following is displayed on the console:
"tempNumber: 0"
When checking on a simple loop:
var tempNumber = 0;

for (i=0; i < 3; i++) {

    tempNumber++;

}

console.log("tempNumber: " + tempNumber);

The correct value "3" is displayed.
Can you please tell me how to output the correct result to the console in the first variant?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2014-05-25
@HaJIuBauKa

At the risk of "meditating": in your <...> the same variable tempNumber changes ...

E
Eugene Obrezkov, 2014-05-25
@ghaiklor

1. You simply change the value of a variable somewhere in the body of the loop back to zero.
2. You use defered, setTimeout, or other tricks to distribute the flow of execution. Which causes the log to fire faster than the processing of the loop body.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question