开发者

Preloading wav/mp3 files in jQuery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜