framework7 icon indicating copy to clipboard operation
framework7 copied to clipboard

Preloader with Delay option

Open eradin opened this issue 8 years ago • 7 comments

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.

eradin avatar Jan 09 '18 16:01 eradin

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();
		}
       });

shastox avatar Jan 09 '18 17:01 shastox

Or use Request Setup https://framework7.io/docs/request.html

shastox avatar Jan 09 '18 17:01 shastox

Aside from the fact that codes doesn't work (preloader will never be visible on setTimeout), why is it a bad idea?

eradin avatar Jan 09 '18 17:01 eradin

Use console and breaks (like "alert") to know where is problem. Or try ... catch.

shastox avatar Jan 09 '18 17:01 shastox

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);

softexe avatar Jan 10 '18 18:01 softexe

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.

eradin avatar Jan 10 '18 21:01 eradin

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

nolimits4web avatar Jan 16 '18 09:01 nolimits4web