最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout not working in Chrome - Stack Overflow

programmeradmin4浏览0评论

This question is for a colleague. He is having an issue in Chrome with the following code snippet:

function showEmailClient(emailContent, url, providerContactId) {
    window.location = emailContent;
    if (providerContactId != undefined) {
        setTimeout(function() {
            clientSideRedirect(url + providerContactId);
        }, 5000);
    }
    else {
        setTimeout(function() {
            clientSideRedirect(url);
        }, 5000);
    }
}​

The setTimeout functions are getting called immediately in Chrome instead of waiting the five seconds they are supposed to. Any ideas?

Update

emailContent is a mailto string, e.g. 'mailto:[email protected]' which causes the user's default mail client to open, not the page to redirect.

This question is for a colleague. He is having an issue in Chrome with the following code snippet:

function showEmailClient(emailContent, url, providerContactId) {
    window.location = emailContent;
    if (providerContactId != undefined) {
        setTimeout(function() {
            clientSideRedirect(url + providerContactId);
        }, 5000);
    }
    else {
        setTimeout(function() {
            clientSideRedirect(url);
        }, 5000);
    }
}​

The setTimeout functions are getting called immediately in Chrome instead of waiting the five seconds they are supposed to. Any ideas?

Update

emailContent is a mailto string, e.g. 'mailto:[email protected]' which causes the user's default mail client to open, not the page to redirect.

Share Improve this question edited Mar 21, 2012 at 17:25 ChevCast asked Mar 21, 2012 at 17:03 ChevCastChevCast 59.3k66 gold badges221 silver badges325 bronze badges 4
  • any error message on the console? – Andreas Wong Commented Mar 21, 2012 at 17:05
  • What are toy trying to do? you're redirecting... I believe this will happen in all browsers the same. – gdoron Commented Mar 21, 2012 at 17:11
  • Works for me: jsfiddle/Vy7cr – gen_Eric Commented Mar 21, 2012 at 17:12
  • 1 Are you sure the immediate call to clientSideRedirect is happening in this function, and not elsewhere? – apsillers Commented Mar 21, 2012 at 17:12
Add a ment  | 

2 Answers 2

Reset to default 5
window.location = emailContent;

This will redirect the browser as soon as this line is hit.

From my experience, don't use use a window.location for bringing up the email client and doing a redirect on the same page - it's a beast. Instead, use a form for submitting the email using the System.Net.Mail namespace and objects in it, then do your redirect. If that isn't an option, store the mailto data in session, redirect and then call the mail client when the page loads. The only caveat about this approach is that the window.location will kill any responses that haven't been written, so you'll need to give the page some time to load using a timer event usually about 2000 milliseconds (if your page has dynamic data and has various load times, good luck!).

This code snippet uses jQuery to wait for the document to be ready for use and adds 2000 milliseconds to ensure any responses are written.

function showEmailClient(mailto)
{
    $(document).ready(function ()
    {
        setTimeout(function ()
        {
            window.location = mailto;
        }, 2000);               
    });
}

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["SendMail"] != null)
    {
        var mailto = Session["SendMail"].ToString();
        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ShowEmailClient", "showEmailClient('" + mailto + "');", true);
        Session["SendMail"] = null;
    }
}
发布评论

评论列表(0)

  1. 暂无评论