Ajax 处理时进度条使用

流程: 前台发起ajax请求,并显示进度条。完成后显示100%,并且clearInterval()

前端代码

<html>
<head>
<meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery UI Progressbar - Default functionality</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
        .ui-progressbar {
            position: relative;
        }
        .progress-label {
            position: absolute;
            left: 50%;
            top: 4px;
            font-weight: bold;
            text-shadow: 1px 1px 0 #fff;
    </style>
</head>
<body>

<button type="button" onclick="start();">开始</button>
<div id="progressbar">
    <div class="progress-label">Loading...</div>
</div>

<script>
    var progressbar = $( "#progressbar" ),
    progressLabel = $( ".progress-label" );
    $(function(){
        if(typeof(inteval) != 'undefined') {
            doClearInterval;
        } else {
            inteval = null;
        }
    })
    // 开始
    function start(){
        //使用JQuery从后台获取JSON格式的数据  
        $.ajax({
            type:"post",//请求方式
            url:"index.php",//发送请求地址  
            // timeout:30000,//超时时间:30秒  
            dataType:"json",//设置返回数据的格式  
            //请求成功后的回调函数 data为json格式  
            beforeSend: function() {
                // 开始进度条
                begeinProgress(10);
            },
            success:function(data){
                console.log(data);
                if(data.code == 0){
                    doClearInterval();
                    setProgress(progressbar, 100);
                }
            },
            //请求出错的处理  
            error:function(){  
                doClearInterval();
                alert("请求出错");  
            }
        });  
    }

    // 进度条长度
    var width = 0;
    function begeinProgress(t) {
        width = 1;
        interval = setInterval("doProgress()", t * 10);
    }
    // 设置进度
    function setProgress(node, width) {
        if (node) {
            progressbar.progressbar({
                value: width,
            });
            progressLabel.text(width + '%');
        }
    }
    // 循环进度
    function doProgress() {
        if(width == 98) {
            doClearInterval();
        }
        setProgress(progressbar, width);
        width++;
    }
    // 清除进度
    function doClearInterval() {
        clearInterval(interval);
    }

    </script>

</body>
</html>

后台代码

<?php
$back['code'] = 0;
$back['data'] = [];
$back['msg'] = 'ok';
sleep(10);
echo json_encode($back);
?>

测试下

file

file

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
39
粉丝
9
喜欢
71
收藏
102
排名:461
访问:1.9 万
私信
所有博文
社区赞助商