【问题标题】:Preloading wav/mp3 files in jQuery在 jQuery 中预加载 wav/mp3 文件
【发布时间】:2010-07-03 08:44:16
【问题描述】:

我有这段代码(从另一个 StackOverflow 问题中找到:

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

此代码运行良好,但我如何修改它以处理多个声音文件?

【问题讨论】:

    标签: jquery html audio mp3 wav


    【解决方案1】:

    嗯。我认为您在这里有两个选择:只需复制+粘贴块即可一次启动多个 $.ajax() 请求:

    $(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();}});   
    });
    

    或者通过将下一个$.ajax() 请求放入前一个的success 回调中来连续运行它们。

    由于浏览器和服务器端的最大连接限制,我倾向于连续运行预加载。打开大量同时连接可能会减慢其他重要元素(如图像、JavaScript 文件和样式表)的加载速度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多