helios_lie
helios_lie

Reputation: 35

settimeout not getting called?

I have a javascript which call a server and get a JSON data which contains some config to enable/disable redirecting to another link. I need to delay the redirection by a few seconds, but it seems that setTimeout() is not getting called in my method. Even if I change redirect() as an anonymous function and pass it in setTimeout it is still not getting called.

<script>
var xhr = new XMLHttpRequest();
var migrationConfig;
xhr.onreadystatechange = function() {   
    function redirect(){
        alert("in redirect");
        window.top.location=migrationConfig.REDIRECT_LINK;
    }       
    if (xhr.readyState == 4 && xhr.status==200) {
        var data = xhr.responseText;
        migrationConfig = JSON.parse(data);
        if(migrationConfig.SHOW_REDIRECT_MESSAGE == 'Y'){
            if (window.confirm(migrationConfig.REDIRECT_MESSAGE)){
                document.body.innerHTML = '';
                document.write("<h1><font color='red'>You will now be redirected to the new URL at:</font></h1>");
                document.write("<h1><font color='red'>"+ migrationConfig.REDIRECT_LINK +"</font></h1>");                            
                setTimeout(redirect,3000);
            }

        }

    }
}
xhr.open('GET', '/MyApp/migration-config?APP_NAME=MyApp', true);
xhr.send(null);

Upvotes: 0

Views: 138

Answers (2)

helios_lie
helios_lie

Reputation: 35

Thanks for all the suggestions. I have improved it as per suggestion by talsibony, and I further found out also that document.write() removes all my content, which makes it unable to find the redirect global variable. So I have instead changed it to add a div element and set the innerHTML. Here is the fixed code in case if someone encountered similar issue.

<script>
var xhr = new XMLHttpRequest();
var migrationConfig;
var redirect;

xhr.onreadystatechange = function() {       
    redirect = function(){
        window.top.location=migrationConfig.REDIRECT_LINK;
    }       


    if (xhr.readyState == 4 && xhr.status==200) {
        var data = xhr.responseText;
        migrationConfig = JSON.parse(data);
        if(migrationConfig.SHOW_REDIRECT_MESSAGE == 'Y'){
            if (window.confirm(migrationConfig.REDIRECT_MESSAGE)){
                document.body.innerHTML = '';
                var div = document.createElement("div");                    
                document.body.insertBefore(div, document.body.firstChild);
                div.innerHTML += "<h1><font color='red'>You will now be redirected to the new URL at:</font></h1>";
                div.innerHTML += "<h1><font color='red'>"+ migrationConfig.REDIRECT_LINK +"</font></h1>";
                div.innerHTML += "<h1><font color='red'>Remember to save the new URL to your favorites</font></h1>";
                setTimeout(redirect,3000);
            }

        }

    }
}
xhr.open('GET', '/MyApp/migration-config?APP_NAME=MyApp', true);
xhr.send(null);

Upvotes: 0

talsibony
talsibony

Reputation: 8746

// set global object for using it inside the settimeout function

var redirect;

and then inside the xhr.onreadystatechange = function() {

redirect = function(){
alert("in redirect");
        window.top.location=migrationConfig.REDIRECT_LINK;
}

setTimeout('redirect()',3000);

Upvotes: 1

Related Questions