0

I am trying this code:

function view_mail_popup_close()
{
     setTimeout("function () { $('#popupbox').fadeOut('slow'); }",200);
     setTimeout("function () { window.location='view_mail.php' }",800);
}

I want to execute it onclick of a link, but only fading function works!

Can someone tell me why my redirect function not working?

1
  • Putting your functions in quotes, just makes them a string, no longer a function Commented Feb 22, 2011 at 16:29

2 Answers 2

3

I'm kind of surprised that either of them works, because you're giving setTimeout a string that defines a function without calling it; if you give setTimeout a string, it essentially does an eval on the string when the timeout occurs, which in theory would create but not call the function. (Edit: And I've confirmed that: http://jsbin.com/uvuje5)

It's almost never correct or necessary to give setTimeout a string; instead, give it a function:

function view_mail_popup_close()
{
     setTimeout(function () { $('#popupbox').fadeOut('slow'); },200);
     setTimeout(function () { window.location='view_mail.php'; },800);
}

Live example

There, the function is created immediately and the reference to it is given to setTimeout, which will call it when the timeout occurs.

(Off-topic: I've also added a missing semicolon at the end of the window.location = statemenet. JavaScript has semicolon insertion, and so the previous version would work, but I strongly advocate never relying on it.)


Update: As Capsule points out, there's a callback on fadeOut that you probably want to use instead of a second setTimeout:

function view_mail_popup_close()
{
     setTimeout(function () {
         $('#popupbox').fadeOut('slow', function() {
             window.location='view_mail.php'; 
         });
     }, 200);
}

Live example

Sign up to request clarification or add additional context in comments.

9 Comments

You could have used the fadeOut callback function to trigger the next setTimeout, just in case it would take some time. Plus, you could even avoid a second setTimeout if you use a delay in the fadeOut.
@Divya: See the update, the code works. If it's not working in your scenario, something else that you haven't shown is interfering.
@Capsule: Agreed, using the fadeOut callback would be the best way to handle this (no need for the second timeout, I'm guessing he's only leaving more time so that the fade has time to complete). Added.
@Divya Mamgai - please tell us how it doesn't work, specifically. stop saying it doesn't work because that's not any useful. does it redirect at all? does it not? does a JS error get thrown in the console? be SPECIFIC
No it only fade outs the table and after sometime table fade in once again!
|
2

You should not be putting "function(){}" in quotes - if you use quotes then put the JS code directly there. What you are doing is syntactically incorrect:

Uncaught SyntaxError: Unexpected token (

Just kill the quotes and feed function literals. You can nest them as such:

function view_mail_popup_close()
{
     setTimeout(function () { 
       $('#popupbox').fadeOut('slow'); 
       setTimeout(function () { window.location.href='view_mail.php' },600);
    },200);
}

If you use quotes, it's slower because it does extra evaluation, and the scope is not kept intact because its defined in global scope, in addition you have to have the DIRECT JS code in there.

If this still doesn't make the page redirect to view_mail.php, please tell us specifically, exactly what happens. If it redirects to a 404/empty page, then you may need to specify root relative, eg href="/view-mail.php" with the leading /.

3 Comments

It is not working for me! What should i do? Please tell me more!
"What should i do?" - please tell us specifically, exactly what happens.
When i execute my script it fade in the table and when i click on the link it fades out but doesnt redirect and on the same time it fade in once again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.