Bogdan
Bogdan

Reputation: 1901

Why is this setTimeout not working & related question inside

I have this script on a page of mine and the setTimeout function never fires. It's just an alert right now but i'm just testing it out. I'm doing a meta refresh on the page just after it if that's any clue, but i've also given that a 10 sec delay so the page isn't refreshed before it's supposed to trigger.

Also, the related question: If I run a javascript with a delay of, say, 10 seconds (with setTimeout) and in that javascript I try to modify a design element that's not on the page when the setTimeout is declared but will be by the time the script is fired. Will it work?

<script language=javascript>

var xmlhttp_get_memento;    
function loop_alerte(){
    setTimeout( function() {
        alert("timeout");
    }, 5000);
    xmlhttp_get_memento = new XMLHttpRequest();
    if (xmlhttp_get_memento==null)
    {
        alert ("Browser does not support HTTP Request (1)");
        return;
    }   
    var url="crm/ajax/get_mementos.php";
    url=url+"?sid="+Math.random();
    xmlhttp_get_memento.onreadystatechange=function() {
        if (xmlhttp_get_memento.readyState == 4) {  
            alert(xmlhttp_get_memento.responseText);                                        
            schimbare_tip_cursor("default");
        }
        else{
            schimbare_tip_cursor("progress");
        }
    };

xmlhttp_get_memento.open("GET",url,true);
xmlhttp_get_memento.send(null);
}

loop_alerte();
</script>';

Upvotes: 0

Views: 1063

Answers (3)

Tim S.
Tim S.

Reputation: 13843

Cleaning up your code would be a nice start. I can imagine a browser doesn't understand the tag <script language=javascript>. I suggest to use <script type="text/javascript"> and if you're lucky, your javascript might work!

Upvotes: 0

karnyj
karnyj

Reputation: 1222

There is nothing wrong with your setTimeout, you will need to debug further
As for your second question -- the function will run, but whatever it is you were trying to do will not work.

Upvotes: 0

peirix
peirix

Reputation: 37731

Your setTimeout looks good, so there's probably something else that's wrong. Have you tried using a javascript debugger to see if you get any errors?

As for your second question, yes, that shouldn't be any problem, as the anonymous function inside the setTimout won't be evaluated until it runs. Live sample here: http://jsbin.com/afonup/2/edit Both with and without jQuery.

Upvotes: 1

Related Questions