Execute Last

"Moran Ben-David" <[email protected]> Mon, 20 Mar 2006 19:11:03 -0500
Newsgroups gmane.comp.web.dom.wdf
Message-ID <001101c64c7b$f139e730$6401a8c0@short>
Hi.

 

Does anyone know of a good way to delay execution of a piece of code without
relying on time (as in setInterval and setTimeout)?

 

In my javascript code I am trying to dynamically generate a piece of code to
run after the current thread of execution is done.  I am trying to do this
via a dynamically created Script object but I'm having no luck.  Does anyone
have an approach to this?

 

My dynamically created script executes as soon as I append it to the
document.  I've tried appending it as a child to the HTML, BODY and HEAD
elements.  The behaviour is the same with all.

 

My test code is below.  The start of execution happens at myonload() where a
counter is manipulated.  After myonload is finished executing, I want the
"Foo.last()" function to run reporting the value of the counter.  I'm trying
to do this in a non-intrusive way.  I cannot modify the onload listener. in
fact, I'm not even sure what event my code (Foo) will be called from.  The
code below is just simple example.  My real application is embedded in a
javascript file that would be used by page I have non-programmatic control
over.

 

The expected behaviour of this test page is that the "last: <count>" alert
has the same count value as the "myonload: <count>" alert.

 

I hope someone has the patience to read this email and offer some help.
Thanks in advance.

 

<html>

<head>

<script>

            var lastExecutors = [];

            function executeLast(o, f)

            {

                        lastExecutors.push([o, f]);

                        createScript("cycleLastExecutors()");

            }

 

            function cycleLastExecutors()

            {

                        var task;

                        while (lastExecutors.length > 0)

                        {

                                    task = lastExecutors.shift();

                                    task[1].apply(task[0]);

                        }

            }

 

            function createScript(code)

            {

                        var codeNode = document.createTextNode(code);

                        var s = document.createElement('script');

                        var h =
document.getElementsByTagName('head').item(0);

 

                        s.appendChild(codeNode);

                        h.appendChild(s);

            }

 

            Foo = function()

            {

                        this.count = 0;

                        executeLast(this, this.last);

            }

 

            Foo.prototype.add = function() { this.count++;}

 

            Foo.prototype.last = function()

            {

                        alert("last: " + this.count);

            }

 

            function myonload()

            {

                        var foo = new Foo();

                        foo.add();

                        foo.add();

                        foo.add();

                        alert("myonload: " + foo.count);

            }

</script>

</head>

<body onload="myonload()">

</body>

</html>

 

 

Moran Ben-David

Placebase

 <http://www.place-base.com> http://www.placebase.com



[Non-text portions of this message have been removed]



Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/