I. Overview
1.1 Problem
In some report, you may need to add a countdown function to the widget to restrict some operations, such as query, export, etc. For example, in the following example, one can only make queries after 14:30 every day.
How to achieve this effect?
1.2 Solution
The main principle is to use the setEnable(true)/setEnable(false) method of the widget to enable/disable the widget. Use the JavaScript timer function setInterval( function(){},time) to get the countdown time and determine whether the countdown is over.
Note: This method does not support mobile devices.
II. Example
Add some widgets to the parameter pane. One of them is the button widget.
Add an after initialization event to the button. Enter the following JS:
var h = 14; //Time limit of HH
var m = 30; //MM
var s = 00; //SS
//Standardize the time
function timeToString(a) {
//HH
var s = '';
s+=parseInt(a / 3600) + 'h';
//MM
s+=parseInt(a % 3600 / 60) + 'm';
//SS
s+=parseInt(a % 60) + 's remaining';
return s;
}
var date1 = new Date();
var date2 = new Date();
//Set time limit
date1.setHours(h);
date1.setMinutes(m);
date1.setSeconds(s);
var d = (date1 - date2) / 1000;
//If available at current initialization, then enable the widget
if(d < 0) {
this.setValue('Query');
this.setEnable(true);
} else {
var btn = this;
//Show countdown
btn.setValue(timeToString(d));
//Disable the widget
btn.setEnable(false);
//Timer
setInterval(function() {
//Reset time
date1 = new Date();
date2 = new Date();
date1.setHours(h);
date1.setMinutes(m);
date1.setSeconds(s);
//Reset time interval
d = (date1 - date2) / 1000;
if(d < 0) {
btn.setValue('Query');
btn.setEnable(true);
} else {
btn.setValue(timeToString(d));
btn.setEnable(false);
}
}, 1000);
}
Save and preview the effect: