Preloader with Delay option
This is a (multiple allowed):
-
[] bug
-
[ X] enhancement
-
[ ] feature-discussion (RFC)
-
Framework7 Version: 2.
-
Platform and Target: All
Currently the preloader function app.preloader.show() immediately displays the preloader icon and blocks all input. Many times we have an ajax request that doesn't take very long and preloader icon will flash for a moment to the user. It would be nice to be able to specify a delay before the preloader displays. Here is an implementation that could be rolled into utility functions but would be nice to be included in F7 preloader component (e.g. app.preloader.show(color, delayms))
var preloader = true;
setTimeout(function(){
if (preloader)
app.preloader.show();
},2000);
app.request({
type : 'get',
url : 'serverCall.php',
dataType : 'json',
success : function(data) {},
complete : function(request, status) {
preloader = false;
app.preloader.hide();
}
});
In addition, it would be nice to disable the block image behind the preloader icon.
Bad idea, because is very simple:
app.request({
type : 'get',
url : 'serverCall.php',
dataType : 'json',
beforeSend : function (xhr) {
setTimeout(function(){
if (!app.preloader.visible) app.preloader.show();
},2000);
},
success : function(data) {},
complete : function(request, status) {
if (app.preloader.visible) app.preloader.hide();
}
});
Or use Request Setup https://framework7.io/docs/request.html
Aside from the fact that codes doesn't work (preloader will never be visible on setTimeout), why is it a bad idea?
Use console and breaks (like "alert") to know where is problem. Or try ... catch.
A bit complex example (not tested). The idea - start timer and request at the same time, if request is ended soon - preloader won't show...
function myRequest(_requestObj, _delayPreloader) {
var self = this;
this.preloaderReadyToDisplay = true;
this.do = function (_delay) {
this.preloaderReadyToDisplay = true;
setTimeout(function () {
self.showPreloader();
}, _delay);
if(_requestObj.hasOwnProperty("complete")) {
_requesObj["complete"] = function (_request, _status) {
self.hidePreloader(_requestObj["complete"],_request,_status);
};
} else {
_requesObj["complete"] = function (_request, _status) {
self.hidePreloader();
};
}
app.request(_requestObj);
};
this.showPreloader = function () {
if(this.preloaderReadyToDisplay) {
app.preloader.show();
this.preloaderReadyToDisplay = false;
}
};
this.hidePreloader = function (_func, _request,_status) {
app.preloader.hide();
this.preloaderReadyToDisplay = false;
if(typeof _func === "function") {
_func(_request,_status);
}
};
this.do(_delayPreloader);
return this;
}
//usage
new myRequest({...},500);
I ended up writing 2 wrapper routines for show and hide. The show is called prior to the request, the hide is called on complete:.
var preloading=false;
function preloaderShow(delay) {
preloading = true;
setTimeout(function(){
if (preloading) app.preloader.show('blue');
}, delay);
}
function preloaderHide() {
preloading = false;
app.preloader.hide();
}
This code works. I still don't see why its a bad idea to incorporate this functionality into the show and hide functions. By the way, the was able to turn the black background off by adding the following style override:
.preloader-modal {background: initial;}
I also think having this option available in the show function is good. Unless there is some IOS/MD standard against it.
It doesn't solve a problem (which is not a problem) at all.
Imagine, you have set a 2000ms delay, and your user on some connection when xhr request to every page gets 2100ms. So such users will still have preloader shown for just a 100ms. I will recommend the following:
- if it is a cordova app doing XHR requests to local files - don't show preloader at all
- otherwise (if these are remote requests) show preloader or progressbar without any delays