JS Button Enabled after Countdown

  • Last update:  2020-12-15
  • 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?

    test.gif


    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.

    image.png

    Add an after initialization event to the button. Enter the following JS:

    image (1).png

    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:

    test.gif

    III. Download template

    Attachment List


    Theme: Parameter
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy