开发者

Preloading wav/mp3 files in jQuery

开发者 https://www.devze.com 2023-01-06 09:22 出处:网络
I have this code (found from another StackOverflow question: $(document).ready(function() { $.ajax(开发者_C百科{

I have this code (found from another StackOverflow question:

$(document).ready(function() {
    $.ajax(开发者_C百科{
        url: "soundfile.mp3",
        success: function() {
            $("#play_button").show();
        }
    });
});

This code works fine, but how can I amend it to handle multiple sound files?


Hmm. I think you have two options here: Either start multiple $.ajax() requests at once by simply copy+pasting the block:

$(document).ready(function() {
  $.ajax({url: "file1.mp3", success: function() {$("#play_button").show();}});
  $.ajax({url: "file2.mp3", success: function() {$("#play_button").show();}});
  $.ajax({url: "file3.mp3", success: function() {$("#play_button").show();}});
  $.ajax({url: "file4.mp3", success: function() {$("#play_button").show();}});   
});

Or run them successively by putting the next $.ajax() request into the success callback of the previous one.

I would tend to run the preloads successively because of the maximum connection limit on both the browser, and the server side. Opening a lot of simultaneous connections could slow down the loading of other important elements like images, JavaScript files and style sheets.

0

精彩评论

暂无评论...
验证码 换一张
取 消