SetTimeout not working in Chrome

This question is for colleagues. He has a problem 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 called immediately in Chrome, rather than waiting for the five seconds they should have. Any ideas?

Update

emailContent is a mailto string, for example. 'mailto: support@somewhere.com ', which causes the mail client to open by default, rather than redirecting the page.

+4
source share
2 answers

From my experience, don't use window.location to attract an email client and redirect to the same page - this is a beast. Instead, use the form to send email using the System.Net.Mail namespace and the objects in it, and then redirect your email. If this is not an option, store the mailto data in a session, redirect and then call the mail client when the page loads. The only caveat about this approach is that window.location will kill any responses that were not written, so you will need to give the page some time to load using a timer event, usually around 2000 milliseconds (if your page has dynamic data and has different loading times, good luck!).

This piece of code uses jQuery to wait for the document to be ready for use, and will add 2,000 milliseconds to record any responses.

 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; } } 
+2
source
 window.location = emailContent; 

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

+5
source

Source: https://habr.com/ru/post/1402698/


All Articles