Picture, audio resource preload and document DOM loading

Source: Internet
Author: User

When you encounter a problem in the project, AJAX requests an audio resource and then dynamically inserts it into the document. The native audio looks really ugly, and each browser has a different style, taking an audio plugin.

Encountered a problem, the request after the direct call plug-in, the resource has not been loaded, unable to get the total length of audio, so after the call time will show nun;

The idea of processing is to wait for the audio resource to execute the plug-in call function after the load is completed

<---picture Resources-
function Loadsprites (src) { var deferred = $. Deferred (); var sprite = new Image (); Sprite.onload = function () { deferred.resolve (); }; SPRITE.SRC = src; return Deferred.promise ();} <--Audio Resource-->function Loadaudios (src) { var deferred = $. Deferred (); var audio = new Audio (SRC); Audio.onloadedmetadata = function () { deferred.resolve (); }; AUDIO.SRC = src; return Deferred.promise ();} How to use var loaders = [];loaders.push (Loadsprites (Img1url)); Loaders.push (Loadaudios (Audiourl)); $.when.apply (NULL, Loaders). Done (function () { $ (' #img1 '). attr ("src", img1url); $ (' #music '). attr ("src", Audiourl);});

This method is to determine the completion of the document loading, loading the execution function

Document.onreadystatechange = Showaudiodata;
function Showaudiodata () {
if (document.readystate = = "complete") {//when page load status
$ (' audio '). CSS (' visibility ', ' visible ');
$ (' audio '). MediaElementPlayer ({
Features: [' playpause ', ' Progress ', ' duration '],
audiowidth:160,
Audioheight:30
});
}
}

Picture, audio resource preload and document DOM loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.