JavaScript模拟实现进度条
1、发起请求。
源码:
* version 1.1.0
假进度条:
实现服务器处理数据的假进度:
当达到某个值之后,若服务器还没有处理完,则停止进度条前进,待服务器处理完之后,结束进度条。
若进度条还没有走完,服务器就已经完成处理,加速走完进度条
参数说明:带*为必须参数
url: “url”,
type: “get”,
okCallback: function(data){
alert(“成功了”);
console.log(data);
process.destroy();
},
errorCallback: function(data){
alert(“失败了”)
console.log(data);
}
});
1.url(*)
发起请求的url
2.type(默认post)
请求类型
3.data(默认{})
参数
4.okCallback ()
成功的回调
5.errorCallback()
失败的回调
6.backColor(默认#eee)
自定义进度条默认颜色
7.processColor(默认#009ed8)
自定义进度颜色
8.stillTime(单位S,默认3)
进度条时间
9.stepTime(单位S,默认.3)
每走一步的间隔时间
10.stepNum(默认10%)0-100之间
每走一步的进度百分比
11.container
装进度条的容器
12:deadline(默认90%)0-100之间
进度条卡死百分比
13:duringTime
进度条走一步的动画时间
依赖说明: 依赖jquery
* */
function ProcessedAjax(option) {
try {
var _this = this.__proto__;
if(!option.url) {
console.error(“url不能为空”);
return;
}
if(option.type && option.type.indexOf(_this.ALLOW_AJAX_TYPE) < 0) {
console.log(“ajax类型不允许”);
return;
};
console.log(_this)
option = _this.arguPrevHandle(option);
_this.settings = $.extend({}, _this.DEFAULT_ARGU, option);
_this.sendToServer();
return {
start: _this.sendToServer,
destroy: _this.destroy
}
} catch(e) {
console.log(“运行时发生了错误:” + e.message);
}
}
//默认参数
ProcessedAjax.prototype.DEFAULT_ARGU = {
type: “post”,
data: {},
okCallback: function(data) {},
errorCallback: function(data) {},
backColor: “#eee”,
processColor: “#009ed8”,
stillTime: 3,
stepTime: .3,
stepNum: 10,
deadline: 90,
duringTime: .3
}
//允许的ajax类型
ProcessedAjax.prototype.ALLOW_AJAX_TYPE = [“get”, “post”, “delete”, “put”];
//传入参数预处理
ProcessedAjax.prototype.arguPrevHandle = function(option) {
option.okCallback = (option.okCallback instanceof Function) ? option.okCallback : undefined;
option.errorCallback = (option.errorCallback instanceof Function) ? option.errorCallback : undefined;
option.stillTime = isNaN(parseFloat(option.stillTime)) ? undefined : parseFloat(option.stillTime);
option.stepTime = isNaN(parseFloat(option.stepTime)) ? undefined : parseFloat(option.stepTime);
option.stepNum = isNaN(parseFloat(option.stepNum)) ? undefined :
(parseFloat(option.stepNum) > 100 ? 100 : parseFloat(option.stepNum) < 0 ? 0 : parseFloat(option.stepNum));
option.deadline = isNaN(parseFloat(option.deadline)) ? undefined :
(parseFloat(option.deadline) > 100 ? 100 : parseFloat(option.deadline) < 0 ? 0 : parseFloat(option.deadline));
option.duringTime = isNaN(parseFloat(option.duringTime)) ? undefined : parseFloat(option.duringTime);
return option;
}
//发送请求
ProcessedAjax.prototype.sendToServer = function() {
//$.ajaxBase(this.settings.type, this.settings.url, this.settings.data, this.successCallback.bind(this), this.defaultCallback.bind(this));
$.ajax({
type: this.settings.type,
url: this.settings.url,
data: this.settings.data,
dataType: “json”,
error: this.successCallback.bind(this),
success: this.defaultCallback.bind(this),
async:true
});
this.showProcess();
this.setProcessInterval();
}
//显示进度条
ProcessedAjax.prototype.showProcess = function() {
$(“.process-percent”).remove();
var processDom =
“<div class=’process-percent’>” +
”
<div class=’process-percent-back’></div>” +
”
<div class=’process-percent-ok’ data-percent=’0′></div>” +
“</div>”;
if($(this.settings.container).length > 0) {
$(this.settings.container).append(processDom);
} else {
$(“body”).append(processDom);
}
this.percent = 0;
}
//销毁
ProcessedAjax.prototype.destroy = function() {
setTimeout(function(){
$(“.process-percent”).remove();
},600);
$(“.process-percent”).css({
“opacity”: “0”,
“transition”: ‘opacity .5s linear’
});
clearInterval(this.processInterval);
this.processInterval = null;
if(this.unrealDeadProcessInterval) {
clearInterval(this.unrealDeadProcessInterval);
this.unrealDeadProcessInterval=null;
}
}
//进度条改变定时器
ProcessedAjax.prototype.setProcessInterval = function() {
this.processInterval = setInterval(function() {
this.forwardProcess();
}.bind(this), this.settings.stepTime * 1000);
//随机假死进度条
this.unrealDeadProcess();
}
//进度条前进
ProcessedAjax.prototype.forwardProcess = function() {
//进度条前进的百分比大于等于成功前的最大百分比,卡死进度条
if(parseFloat(this.percent) >= parseFloat(this.settings.deadline)
|| (parseFloat(this.percent) + parseFloat(this.settings.stepNum)) >= parseFloat(this.settings.deadline)) {
this.deadProcess();
return;
}
this.setProcessPercent(parseFloat(this.settings.stepNum),.1);
}
//设置进度条百分比
ProcessedAjax.prototype.setProcessPercent = function(percent, time) {
this.percent=parseFloat(this.percent)+parseFloat(percent);
this.percent=parseFloat(this.percent)>100?100:parseFloat(this.percent);
$(“.process-percent .process-percent-ok”).attr(“data-percent”,this.percent);
var allWidth = $(“.process-percent .process-percent-back”).outerWidth(true);
$(“.process-percent .process-percent-ok”).css({
width: allWidth * (this.percent / 100),
transition: “width ” + time + “s” + ” linear”
});
}
//卡死进度条
ProcessedAjax.prototype.deadProcess = function(isError) {
clearInterval(this.processInterval);
this.processInterval = null;
if(isError) {//保证因为失败引起的卡死不会重启定时器
if(this.unrealDeadProcessInterval) {
clearInterval(this.unrealDeadProcessInterval);
this.unrealDeadProcessInterval=null;
}
}
}
无论如何走完进度条
ProcessedAjax.prototype.processToAllAnyway = function() {
this.setProcessPercent(100,.3);
}
//随机假死进度条,假死时间1-10s随机
ProcessedAjax.prototype.unrealDeadProcess = function() {
this.unrealDeadProcessInterval = setInterval(function() {
if(this.percent === 0) {
} else {
this.deadProcess();
clearInterval(this.unrealDeadProcessInterval);
this.unrealDeadProcessInterval=null;
this.reuseProcessInterval();
}
}.bind(this), this.getRandomNum(1,10)*1000);
}
//重启进度条前进定时器,前进百分比随机
ProcessedAjax.prototype.reuseProcessInterval = function() {
var addPercent=this.getRandomNum(0,100-parseFloat(this.percent));
this.setProcessPercent(addPercent, .3);
this.setProcessInterval();
}
//产生变化随机数
ProcessedAjax.prototype.getRandomNum = function(min,max) {
var numArea = max – min;//大小范围
var rand = Math.random();//随机数
return(min + Math.round(rand * numArea));
}
//成功的回调
ProcessedAjax.prototype.successCallback = function(data) {
this.processToAllAnyway();
setTimeout(function(){
this.settings.okCallback(data);
}.bind(this),500);
}
//失败的回调
ProcessedAjax.prototype.defaultCallback = function(data) {
this.deadProcess(true);
this.settings.errorCallback(data);
}
今天的文章JavaScript模拟实现进度条分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/29446.html