jQuery ajax() preloading multiple things

不羁岁月 提交于 2019-12-21 06:28:24

问题


I am using the following code to preload an mp3:

$.ajax({
    url: "boom.mp3",
    success: function() {
        //done
    }
});

Is there anyway I can have multiple elements preloaded (images and mp3 for example)?

e.g.

$.ajax({
    url: "boom.mp3", "moo.jpg",
    success: function() {
        //done
    }
});

Cheers!


回答1:


If you are using jQuery 1.5, there are two new ways to handle this: deferred and promises.

Creating Responsive Applications Using jQuery Deferred and Promises

function successFunc(){
  console.log( “success!” );
}    

function failureFunc(){
  console.log( “failure!” );
}

$.when(
  $.ajax( "/main.php" ),
  $.ajax( "/modules.php" ),
  $.ajax( "/lists.php" )
).then( successFunc, failureFunc );

To use this in your instance, for example, just replace the ajax requests with load.




回答2:


An idea from me: Collect all filenames in an array and loop through the array. Save th received opjects in another array and use it for what you want.

Perhaps it will work if you define the object in the url parameter, seperated by colon.




回答3:


<script>
$.ajax({
    type:"GET",
    url:'',
    dataType:'json',
    async:false,
    beforeSend:function(data){ // Are not working with dataType:'jsonp'
      $('#content').html('Loading...');
    },
    success:function(data){
        $('#content').html(data.content);
    }
});
</script>

Read beforeSend(jqXHR, settings)



来源:https://stackoverflow.com/questions/5511549/jquery-ajax-preloading-multiple-things

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!