Skip to content

Commit ed96880

Browse files
BR0kEN-simonbrunel
authored andcommitted
Ensure chart.ctx exists during deferred updates (#14)
Ensure the chart instance is still alive. It may have been destroyed during a delay and calling chart.update() will fail. The most common reason for such scenario is user navigation. Steps: - open a view with the chart that has a deferred render; - close a view before the delay timer expires. After the second step both chart.ctx and chart.canvas won't exist. Note that chart.canvas is a reference of chart.ctx.canvas.
1 parent 82144df commit ed96880

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/plugin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,14 @@ Chart.plugins.register({
155155
if (options.delay > 0) {
156156
model.delayed = true;
157157
defer(function() {
158-
model.delayed = false;
159-
chart.update();
158+
// Ensure the chart instance is still alive. It may have been destroyed
159+
// during a delay and calling `chart.update()` will fail. The most common
160+
// reason for such scenario is user navigation.
161+
// https://github.com/chartjs/chartjs-plugin-deferred/pull/14
162+
if (chart.ctx) {
163+
model.delayed = false;
164+
chart.update();
165+
}
160166
}, options.delay);
161167

162168
return false;

0 commit comments

Comments
 (0)