Javascript - 加载内容、回调和加载

Javascript - Load content, callback and loading

本文关键字:加载 回调 Javascript      更新时间:2023-09-26

这是我的"getContent"函数,它需要一个URL:

function getContent(url, callback) {
    var request = new Sys.Net.WebRequest();
    request.set_url(url);
    request.set_httpVerb("GET");
    var del = Function.createCallback(getContentResults, callback);
    request.add_completed(del);
    request.invoke();
}

我想做的是在调用此图像时显示加载图像,然后在回调完成后将其隐藏?

有人可以建议安汀吗?

您可以使用 jQuery .load() 函数加载 HTML,然后使用 .ajaxStart() 和 .ajaxStop() 事件处理程序来显示和隐藏加载动画。

下面是一个示例:

$(document).ready(function() {
    $("#loadlink").click( function() {
        $("#container").load( "load.htm" );
    });
    $("#loadlink").ajaxStart( function() {
        console.log("start");
    });
    $("#loadlink").ajaxStop( function() {
        console.log("stop");
    });
});
$('#selector').html('<img src="loading.gif" />').ajax({
   URL: 'myurl',
   type: 'GET',
   dataType:'html',
   success:function (data){
        console.log('This has changed');
        // data is your html returned/response
   },
   error: function (){
        //this handles errors such as 404,500, etc...
   }
});

这是怎么回事?

$('#selector')//is where the result would be shown this instance <div id="selector">
    .html('<img src="loading.gif" />')// adds an image to <div id="selector"><img src="loading" /></div>
    .ajax();//well its the ajax call to get the page data

欲了解更多信息

http://api.jquery.com/jQuery.ajax/