Black Lives Matter. Please consider donating to Black Girls Code today.

How to turn off an interval


I’m trying to use a interval in my application in order to trigger an clyclical event and later stop it but I’m having some problems stopping the interval.

I have searched and found this thread in the forum that seems related:

but it seems to have been solved including disabled property according to this thread:

This is an example of how i’m using the disabled property:

import dash_core_components as dcc
import dash_html_components as html
import dash
from dash.dependencies import Input, Output, State

app = dash.Dash()

app.layout = html.Div([
    html.Div('Sampling time (s):',style  = {'width': '10%', 'display': 'inline-block',
                                                           'fontFamily':'Segoe UI','textAlign':'center',
    dcc.Input(id='SamplingTime', value= 1, type='number',style={'width': '5%', 'display': 'inline-block',
                                                                    'fontFamily':'Segoe UI','textAlign':'center',
                                                                    'fontSize':'16px','borderWidth': '1px',
                                                                    'borderStyle': 'solid','borderRadius': '5px',
    html.Button('REGISTER',id ='RegisterButton', n_clicks_timestamp='0',
                                    style = {'width': '12%','fontFamily':'Impact, Charcoal, sans-serif',
                                             'fontSize':'16px','borderWidth': '1px','borderStyle': 'solid',
                                             'borderRadius': '3.5px','borderColor':'#ccc','background':'#000',
    html.Button('Stop',id ='StopButton', n_clicks_timestamp='0',
                                    style = {'width': '12%','fontFamily':'Impact, Charcoal, sans-serif',
                                             'fontSize':'16px','borderWidth': '1px','borderStyle': 'solid',
                                             'borderRadius': '3.5px','borderColor':'#ccc','background':'#000',
    dcc.Interval(id='SamplingInterval', interval = 0, disabled = True, n_intervals = 0),

@app.callback(Output('SamplingInterval', 'interval'),
                [Input('RegisterButton', 'n_clicks')],
                [State('SamplingTime', 'value')])

def DefineSamplingTime (RegisterOrder, SamplingTimeValue):
        if RegisterOrder > 0:
            return SamplingTimeValue*1000
        else: return 0
    except Exception as e:

@app.callback(Output('SamplingInterval', 'disabled'),
                [Input('RegisterButton', 'n_clicks_timestamp'),
                 Input('StopButton', 'n_clicks_timestamp')])
def EnableDisableInterval (RegisterOrder, StopOrder):
        if int(RegisterOrder) > int(StopOrder):
            return False
        if int(StopOrder) > int(RegisterOrder):
            return True
        if int(RegisterOrder) == int(StopOrder):
            return True
    except Exception as e:
@app.callback(Output('RegisterStatus', 'children'),
                [Input('SamplingInterval', 'interval'),
                 Input('SamplingInterval', 'disabled'),
                 Input('SamplingInterval', 'n_intervals')])

def ShowRegisterStatus (SamplingTime, Stopped, cycles):
    if SamplingTime == None: Tiempo = 0   
    else: Tiempo = SamplingTime/1000
    if Stopped == None:
        Estado = 'stopped'
    elif not Stopped:
        Estado = 'registering'
    else: Estado = 'stopped'
    msg = 'the register is  {} with a sampling time = {} s for {} cycles'.format(Estado, Tiempo, cycles)
    return html.Div([
if __name__ == '__main__':

propably i’m doing samething wrong but i can’t find where the error is because de property ‘n_intervals’ keep increasing even if the property ‘disabled’ is set to TRUE

The disabled prop doesn’t work, it’s an open issue on the dash-core-components repo.

Best workaround at the moment is to simply set interval to something very large in your callbacks, so that the interval fires e.g. once a day and is effectively disabled.

1 Like