Forum Moderators: open

Message Too Old, No Replies

How to write window messages in proper order

Messages currently display at program end

         

plexml

10:04 am on Dec 14, 2008 (gmt 0)

10+ Year Member



I need messages to a popup window to appear in real time right away but instead they all appear at the end of the javascript program. There are two popup windows and a 3 second delay between them. The messages to each popup all appear after the 3 second delay whereas I want the first popup message to appear in order before the 3 second delay (as you might think it should).

It only does it for Firefox, in IE there is no problem.

Is there any workaround or other way to get the messages to appear as they happen?

Here is the code of the demo:-

<script>

function wait(ms){ // Wait for ms millisecs
var start = new Date()
while( ((new Date())-start)<ms ) {}
return}

function test() {
w1 = window.open("", 'w1'+(new Date()).getTime(), "width=200,height=200,screenX=100,left=100,screenY=100,top=100");
w1.document.write("First Window<BR>");
w1.document.close()
// Why doesn't Firefox display the text "First Window" before the delay? IE and Netscape 4.79 do.
wait(3000)
w2 = window.open("", 'w2'+(new Date()).getTime(), "width=200,height=200,screenX=200,left=200,screenY=200,top=200");
w2.document.write("Second Window<BR>");
w2.document.close()
// Firefox waits until the end before displaying the text "First Window".
// This means things are getting done out of order.
return}

</script>

<br>
<a href="javascript:void(0)" onClick="test();return false"> Start the test</a><br>

wyweb

5:29 am on Dec 17, 2008 (gmt 0)



what?

daveVk

1:01 pm on Dec 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cleaner way would be

function test() {
window.setTimeout("doWindow(1)", 1); // or just doWindow(1) ?
window.setTimeout("doWindow(2)", 3000);
}

where doWindow has open + write

Problems I see with your code
- wait hogs javascript for 3 seconds preventing other things like opening window from happening
- you close each window immediately after opening it ?

Welcome to the forum

plexml

6:11 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



Yes that does get the two windows displaying in proper sequence thanks, but the wait(3000) is crucial and has been removed. My window example doesn't show my problem properly, sorry.

My real problem is logging to a single popup window. My log program works fine on Netscape 4.79 and in IE6 but won't work in Firefox. By "works fine" I mean log messages are displayed in real time as they happen rather than all at the end as for Firefox.

The wait(3000) simulates the time taken for various other programming tasks which come between log messages.

You say "wait hogs javascript for 3 seconds preventing other things like opening window from happening" but really the line w1.document.write("First Window<BR>") comes before the wait(3000) so you would expect the text "First Window" to appear before too. Indeed it does for IE6 but not Firefox. Maybe Firefox is not obeying commands in the right order. Maybe it treats window operations differently from normal operations, I don't know but whatever it is I have a big headache since the log is no good at all if messages are not in real time.

So the problem is better shown like this:-

function test() {
doWindow('first log message')
wait(3000)
doWindow('second log message')
}

function doWindow(txt) {
if(typeof w1=="undefined" ¦¦ !w1 ¦¦ w1.closed) {
w1 = window.open("", 'logwin')
}
w1.document.write(txt + "<BR>");
}

Try it in IE and then in Firefox to see the difference. IE6 (probably IE7 too) displays the text "First Window" well before the text "Second Window" appears. With Firefox all text displays together after 3 seconds.

I tried setTimeout:-

function test() {
setTimeout("doWindow('first log message')", 1)
wait(3000)
setTimeout("doWindow('second log message')", 1)
}

but it doesn't work. All messages display at the end for both Firefox and IE. I am beginning to think there is no solution.

Thanks very much for your help.

daveVk

2:12 am on Dec 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but really the line w1.document.write("First Window<BR>") comes before the wait(3000)

Ideally you are correct, but the new window I think has its own javascript tread running asynch to main thread. For whatever reason as you say things happen out of order.

but the wait(3000) is crucial and has been removed.

this line says wait 3 secs then do doWindow(2)
window.setTimeout("doWindow(2)", 3000);

Don't understand why this is not a suitable substitute, unless you want the 3 seconds delay to occur after window 1 closed or something else ?

plexml

1:01 am on Dec 19, 2008 (gmt 0)

10+ Year Member



The wait(3000) is supposed to simulate javascript program operations that take a significant amount of time. Such operations could include downloading or uploading network files and many local file operations too (I use liveConnect plus an applet). Sometimes we are uploading hundreds of files and need a proper log to show real-time progress.

Unfortunately my example is poor because the 3 second wait can be added to the setTimeout as you have done to get a solution.

Please have a look at this example which does not use a simple delay as before. I don't think the setTimeout method can be used here:-

function test() {
doWindow('first log message')
slowFunc(17, 600)
doWindow('2nd log message')
slowFunc(17, 600)
doWindow('end of log messages')
}

function doWindow(txt) {
if(typeof w1=="undefined" ¦¦ !w1 ¦¦ w1.closed) {
w1 = window.open("", 'logwin')
}
w1.document.write(txt + "<BR>");
}

function slowFunc(stringSize, numloops) {
var str="absdefg", i, ix
// Make a large string with "1234" in the middle.
for(i=0; i<stringSize; ++i) {
str += str
}
str = str + "1234" + str
// Find "1234" repeatedly
for(i=0; i<numloops; ++i) {
ix = str.indexOf("1234")
}
doWindow('slowFunc done')
}

If you try it in IE and Firefox you can see the different behaviour. In IE the log messages come out one by one in as they happen but in Firefox they all come at the end. What I need is some way for Firefox to display the log messages in real-time too.

daveVk

1:32 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case

function test() {
window.setTimeout("doWindow('first log message')",1);
window.setTimeout("slowFunc(17, 600);doWindow('2nd log message');skedLast()",5);
}
function skedLast(){
window.setTimeout("slowFunc(17, 600);doWindow('end of log messages'),1 );
}

plexml

2:04 am on Dec 19, 2008 (gmt 0)

10+ Year Member



I'm not quite sure how it's working but it does work in all browsers. GREAT!

Now I've just got to convert several years of code to setTimeout!

Would prefer if Firefox sorted it their end really but I don't suppose they will. Everything was fine on Netscape 4.79...

Many thanks, you surprised me.