Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14447

Re: XMLHttpRequest, using setTimeout() from within child

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: XMLHttpRequest, using setTimeout() from within child
Date 2012-06-11 12:11 +0200
Organization albasani.net
Message-ID <jr4g80$7k2$1@news.albasani.net> (permalink)
References <e9f70f93-4748-4350-bd32-476963d34320@googlegroups.com>

Show all headers | View raw


On 2012-06-11 06:09, Jason C wrote:
> I'm using XMLHttpRequest to load and show a simple PHP script.
> 
> How can I use setTimeout() from within that PHP script without an
> onClick event? Or does it have to be triggered from the parent?
> 
> I'm trying to run this so that after the script loads, it will
> "close" (or, set the ID to "&nbsp;", which looks like it closed)
> after 5 seconds:
...

You're leaving a lot of information out of your question, so I'll have
to guess what you're doing. If my guess is incorrect, please be more
specific.

I'm assuming you're using XHR to insert a piece of HTML into the
document via innerHTML (whether or not the HTML is static or generated
by PHP is irrelevant). This piece of HTML contains a script element, and
you want to know why the setTimeout function in that script doesn't work.

If that's correct, then the answer is that scripts inserted with
innerHTML are never evaluated. So the problem isn't the setTimeout
function, but the way you add the script. I would suggest that you
execute the "setTimeout(...)" statement from the same function which
inserts the returned HTML, i.e. the onreadystatechange callback.

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            myTarget.innerHTML = xhr.responseText;
            setTimeout(...); // your code here
        }
    };

If you're using a library for XHR, they usually have a way to add custom
callbacks for the success/error cases.

Another (less elegant) option would be to scan the inserted HTML for
script elements and manually evaluate them.


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

XMLHttpRequest, using setTimeout() from within child Jason C <jwcarlton@gmail.com> - 2012-06-10 21:09 -0700
  Re: XMLHttpRequest, using setTimeout() from within child "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-11 09:42 +0000
  Re: XMLHttpRequest, using setTimeout() from within child Stefan Weiss <krewecherl@gmail.com> - 2012-06-11 12:11 +0200

csiph-web