如何正确销毁jQuery flot图形,以便清除所有事件处理程序并避免内存泄漏?
似乎flot会留下一些僵尸(也称为Detached Dom Trees)。
如果您阅读API文档,会发现有一个关闭方法可以为您清理。
shutdown()
Cleans up any event handlers Flot has currently registered. This
is used internally.
eg.
var plot = $.plot($("#yourDiv"), options)
plot.shutdown()
对于未来来到这里的人,现在有一个destroy
函数,它会调用关闭和移除画布元素。由于某些原因,它在API文档中没有记录,但在代码中是可用的:
var flot = $("#FlotDiv").data('plot')
if (flot) // If it's destroyed, then data('plot') will be undefined
flot.destroy();
如果你想删除事件处理程序,可以尝试使用jQuery off
方法。
如果要清除 flot 图表,则可以清空 div。
$('#yourFlotDiv').empty();
要移除flot图表
var placeholder = $("#FlotDiv");
placeholder.unbind(); //Remove a previously-attached event handler from the elements.
placeholder.empty();
如果您想要解除绑定特定事件,可以使用以下方法:
$( "#foo").unbind( "click" );
欲知详情请查看this。