How to turn off an interval

Hello

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:

https://community.plotly.com/t/how-to-turn-off-interval-event/5565

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

https://github.com/plotly/dash-core-components/pull/66

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',
                                                           'fontSize':'16px',}),
                        
    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',
                                                                    'borderColor':'#ccc',}), 
    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',
                                             'cursor':'pointer','color':'#fff'}),
    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',
                                             'cursor':'pointer','color':'#fff'}),
    dcc.Interval(id='SamplingInterval', interval = 0, disabled = True, n_intervals = 0),
    html.Div(id='RegisterStatus'),
])

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

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

@app.callback(Output('SamplingInterval', 'disabled'),
                [Input('RegisterButton', 'n_clicks_timestamp'),
                 Input('StopButton', 'n_clicks_timestamp')])
 
def EnableDisableInterval (RegisterOrder, StopOrder):
    try:
        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:
        print(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([
        html.Div(msg)
        ])
if __name__ == '__main__':
    app.run_server(debug=True)

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