1. 程式人生 > >Web前端優雅的顯示網路請求進度條

Web前端優雅的顯示網路請求進度條

Java程式設計師寫Js我就是喜歡寫“;” 沒辦法。好久沒寫android了,最近是後臺前端都在搞,尤其喜歡$(this),記錄每一天的生活

httpUtils.js如下

/**
 * 網路請求
 * @author jiangrongtao
 * Created by on 2017-10-8.
 */
/**
 * 訪問方式 get 和 post
 * @type {string}
 */
var REQUEST_POST='post';
var REQUEST_GET='get';
/**
 * 超時時間
 */
var OUT_TIME_LEVAl_1=1000;
var OUT_TIME_LEVAl_2=2000
; var OUT_TIME_LEVAl_3=3000; /** * * @param url 請求路徑 * @param timeout 超時時間 * @param data 提交引數 * @param successCallBack 請求成功的回撥 * @param errorCallBcak 請求失敗的回撥 * @param msg 進度條顯示的資訊 * @param requestType 請求方式get post */ function httpAjax(url,timeout,requestType,data,successCallBack,errorCallBcak,msg){ var
isShow=false; //顯示進度條 if(!isShow){ showLoading(msg); isShow=true; } $.ajax({ url:url, timeout : timeout, type : requestType, data :data, dataType:'json', success:function(result){ // 去掉進度條 if(isShow){ hiddleLoading(); isShow=false
; } console.log("success"); successCallBack(result); }, error:function (XMLHttpRequest,errorMsg,ex) { // 去掉進度條 if(isShow){ hiddleLoading(); isShow=false; } console.log("error"); errorCallBcak(errorMsg); } }); } /** * 顯示進度條 * @param msg 進度條下面顯示的內容 */ function showLoading(msg) { var ahtml="<div class='loading'> <div class='pic'> <img src='image/loading.svg' width='80px' height='80px' ><br>"+msg+"</div></div>" ; $("body").append(ahtml); } /** * 隱藏進度條 */ function hiddleLoading() { $(".loading").fadeOut(100); }

具體的使用場景 一般在onclick中

     $(document).ready(function(){
          httpAjax(SELECT_USER_BY_ID,OUT_TIME_LEVAl_3,REQUEST_GET,null,
                function successCallBack(result) {
                    console.log(result);
                    initData(result.status);
                },function errorCallBcak(errorMsg){
                    var errorContent = '<p>請求服務失敗,請稍後<a href="xxx.html" style="color:blue">重試</a>!</p>';
                    $("body").html(errorContent);
                },'載入中...');
        });

未完,待擴充!