www.webdeveloper.com
Results 1 to 6 of 6

Thread: Detect when JavaScript has finished executing...

  1. #1
    Join Date
    May 2010
    Posts
    24

    Detect when JavaScript has finished executing...

    Is there a way to detect when the result of a JavaScript script have finished executing.

    For example, if I have a (browser-side) Javascript function that fills in a text field, or submits a form, how do I know when the page is finished changing?

    Again my only idea is to check the whole DOM tree with setInterval... but this seems quite tedious.

    Any better solutions?

    Thank you
    Misha

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    Its the return true statement of the function that signals its completion
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    	"http://www.w3.org/TR/html4/strict.dtd">
    <HTML>
    <HEAD><TITLE>Th6-21pm52</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=UTF-8">
    <STYLE type=text/CSS>
    
    
    </STYLE>
    <SCRIPT type="text/javascript">
    function myFunction(){
    for(i=0;i<document.forms[0].elements.length;i++){document.forms[0].elements[i].value=i}
    return true
    }
    </SCRIPT>
    <META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
    <BODY scroll="auto" onload="if(myFunction()){alert('done')}">
    <form>
    <input><input><input><input><input>
    </form>
    
    </BODY></HTML>

  3. #3
    Join Date
    May 2010
    Posts
    24
    Thank you. I guess let me be more specific.

    I am on the client (browser side).

    I execute a Javascript (say one that launches events).

    Sometimes, this Javascript, for example, causes a new page to load, or causes new DOM elements to be created (clicking on the "Check" menu in Google Voice, or posting a Wall Post on Facebook).

    I would like to know when all the _consequences_ of running my script have completed.

    My simple idea:
    * look at document.getElementsByTagName('*').length every 2 seconds (?)
    * if it has not changed - we are done. otherwise, wait again

    What do you think?

    Misha

  4. #4
    Join Date
    Oct 2006
    Posts
    939
    If I knew how to write a loop that would take many seconds to run,
    the return would not return until the code is finished running.
    I stand by my earlier example.
    Perhaps you missed the crucial bit-
    if(myFunction()){alert('done')}

  5. #5
    Join Date
    May 2010
    Posts
    24
    Perhaps you are correct.

    My challenge is actually to find whether my Javascript caused any DOM changes, and the only cross-browser way I believe to do this is to do a for loop that scans all elements...

    Unless you have other suggestions.

    Thank you
    Misha

  6. #6
    Join Date
    Oct 2006
    Posts
    939
    You'll need to post your code before anyone can show a better way to do it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles