JS within panel to set dashboard refresh interval

I have a similar question to Javascript/jQuery event on regular refresh intervals?. That was about how to “catch” a refresh event from a JavaScript snippet in an HTML panel. I would like to kind of do the opposite - i.e. I’d like to set the dashboard auto-refresh interval to a particular value from a similar script.

Very naively, inspired by the other discussion as well as Present Time Range in Grafana's Text Panel - Stack Overflow, I tried

angular.element('grafana-app').injector().get('timeSrv').refresh = '5s';

While the value was set successfully, it did not have any effect on the actual dashboard behavior.

I suspect that the above is no longer a viable approach in 7.x, with the migration to React? Is there still a reasonable way to achieve what I’m attempting?

1 Like

@daniellee I figured you might know the answer. Any pointers?

Tough question - I will ask the frontend experts in the team for a better answer as your approach is using Grafana internals that are subject to change in the future.

Did you test using the setAutoRefresh function in the TimeSrv class instead? There is also a refreshDashboard() function to manually trigger a refresh.

Thanks a lot for letting me know - if it’s not straightforward then I may be able to find another way to achieve what I’m after.

In fact, having an internal timer in the custom JS that periodically triggers refreshDashboard() may well do the trick here. Will give that a go.

1 Like

Just to confirm that

angular.element('grafana-app').injector().get('timeSrv').setAutoRefresh('5s')

is exactly what I was after. Thanks for the tip @daniellee, not sure how I missed that. (and yes…I’m fully aware this may break in the future…)

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.