2

I am trying to figure out, how to write to a div in "real time" from javascript. When I run the code, any text from the loop is shown only once both loops have executed, but really the way (I think I wrote it), it should be popping out line by line.

See an example .JsFiddle Demo

I would like it to work just like in a chrome developer tool console, where you see right away what is happening (using console.log).

I know I could put it all to a string or array and print it in one go outside the loops, but I really need to see it as it is happening.

I am sure there must be already a solution somewhere, but I just can't find any leads (maybe not using the right words for google). Any info would be highly appreciated.

EDIT: Made output in console and HTML the same, to make it less confusing and allow for better comparison JSFiddle Demo 2

5
  • What do you mean by "right away"? It is actually happening "right away", but it's actually so fast that you cannot see it. The while cycle is happening and is printing console messages one by one along with div elements, however it does it so fast that it looks like it is doing that immediatly. Do you want to add some sort of a delay between each of them? Commented Jul 22, 2016 at 10:34
  • There are three loops running out.. where you want to print string? Commented Jul 22, 2016 at 10:34
  • @briosheje: Sorry for not being clear: No matter how many repeats you add to the loop, you can watch the console.log popping up one after the other. Yet the appendTo (as one of the possibilities) does not seem to be doing that. I see all the strings only as one, once the whole code is done executing. Commented Jul 22, 2016 at 10:40
  • this is because you are using console.log inside the loop... If output is same every time then console wrapped it and show no. of results as prefix. Commented Jul 22, 2016 at 10:42
  • I am using appendTo also inside the loop. So following that logic, both should be just printing close to the same time. But that is not the case. Increase generateTestCases to 10.000 in the JsFiddle and watch the console and the div at the same time. Updatedt the JsFiddle to make it more clear jsfiddle.net/John1234/77pt5686/3 Commented Jul 22, 2016 at 10:45

1 Answer 1

2

The problem is, that your while loops are to fast. You would not see the output live. The browser needs some time to update the document. The reason you can see it each-by-each in the console, is just because the console is to slow. The console outputs the lines with a delay. When you see the last entry in your console is comming up, your code has already ended a long time before. Your code execution is mutch faster than the console. So this is not in time too.

It is pretty easy to show how slow the console really is. Take a look at this example. When executed there is a simple loop creating log entries in the console. After that we just print loop finished to the content. When you see the output in the document, the console still print lines, even if the script has already stopped.

You could now try to give the browser some time in your loop execution, to output the data and let the browser render the change. But because you used loops there, you can't make it async or create a delay to output the values first, before starting with the next one. But you can switch the code to self executing functions and trim down the time a bit for a smooth output in the browser before printing the next line.

And if you create such big loops, you should really take care of your code. It is not a good idea to let jQuery search the #log div in every loop again. Just do it once before the loops. And don't use an array for this simple string operations. This slows it down even more.

var log = $("#log");
var generateTestCases = 3000;

(function next(amount) {
    if( amount ) {
        var repeatIF2 = 10;
        var trackingString = amount + ";";

        (function output(count) {
            if( count ) {
                trackingString += count + ";";
                log.append('<div>' + trackingString + '</div>');

                if( --count ) {
                    // this line slows down the output
                    // change the time for faster or slower output
                    setTimeout(output, 50, count);
                } else {
                    next(--amount);
                }
            }
        })(repeatIF2);
    }
})(generateTestCases);

Working example.

Sign up to request clarification or add additional context in comments.

11 Comments

the result is exactly what I was looking for. I just set the setTimeout to 0, as all I care about is that I see that the code is being executed. Can you maybe elaborate a little more on "while loop are too fast"? In my old code, I see line after line in the console, but nothing till the end in the browser. And in the end, I get a dump of lines in the browser all at once, while the console is still finishing. But if the while loops are so fast, why can't they print to the site a little faster than to the console? It is both in the same loop....
Please read the first block of my answer again. The console is not in time, the console is just to slow. The console print out the lines much slower than your loop are execute it. When you see the last line in your console, your real execution ended a long time before that ...
I created a second example for you, @TheRealPir, to prove that the console is really slow. Take a look at my original answer in the new second paragraph.
Thanks @eisbehr for the example. Let's agree that the console is just slow. Done. Yet, what I am still struggling to understand, is why the webpage, in my old code, does not show any in-between lines, only a final "all lines at once" dump. Granted, even if it is faster than the line by line way of the console. I am not after "in time", rather than "line by line" output understanding. I assume it has something to do with your statement of "while loops being too fast". Is the page updating and updating very fast, thus being white, or what is the reason?
@TheRealPir: One more time: the webpage is NOT showing a "all lines at once" dump, it's showing one line at a time, but it's doing it so fast that it literally looks like it's doing it in one shot only.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.