简体   繁体   中英

javascript - abort script execution timeout?

I want to improve the load time of my web site, which contains scripts which are loaded (Tracking / Ads / Other).

Some of the scripts I host for 3rd parties have unexpected load time which vary from milliseconds to X seconds.

Now, I do not intend to educate my vendors, but was wondering if I can set a timeout and abort the script if it had not completed execution after X time?

Suggestions and info will be appreciated.

A better approach would be using the async attribute on you script tags like this:

<script src="vendor.com/script.js" async></script>

From MDN docs:

This is a Boolean attribute indicating that the browser should, if possible, execute the script asynchronously.

That means your page will continue to load and download other assets without blocking. This is of course works only if you are not depends on the vendor's script.

For instance, if your website need jQuery to render the basic content and you used async on the jQuery script tag, it will fail since the jQuery script might not be ready yet. I assumed that not your case since you wanted to abort the script.

Also, It has pretty good browser support too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM