2๐
โ
A variable declared with var
is hoisted to the top of the function. You are declaring the same variable again in the function.
So the function declaration is hoisted to top of function where it is undefined.
Hence, myChart.destroy()
is undefined.
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
Instead of actually destroying the instance, you should update the instance instead.
myChart.data.datasets = [] //Desired Data
myChart.update();
If you want to go with destroying the instance, you can remove the var
declaration from inside the function and it should work fine. (since the variable is already defined in scope of that function.
Source:stackexchange.com