摧毁jQuery Flot图表

8

如何正确销毁jQuery flot图形,以便清除所有事件处理程序并避免内存泄漏?

似乎flot会留下一些僵尸(也称为Detached Dom Trees)。


$ .plot('#yourFlotDiv',{},{});”我在某个地方看到过它,不太记得在哪里了。 - Ejaz
4个回答

7

如果您阅读API文档,会发现有一个关闭方法可以为您清理。

shutdown()

Cleans up any event handlers Flot has currently registered. This
is used internally.

eg.

var plot = $.plot($("#yourDiv"), options)

plot.shutdown()

4

对于未来来到这里的人,现在有一个destroy函数,它会调用关闭和移除画布元素。由于某些原因,它在API文档中没有记录,但在代码中是可用的:

var flot = $("#FlotDiv").data('plot')
if (flot) // If it's destroyed, then data('plot') will be undefined
    flot.destroy();

3

如果你想删除事件处理程序,可以尝试使用jQuery off方法。

如果要清除 flot 图表,则可以清空 div。

$('#yourFlotDiv').empty();

jQuery.off() 应该传递哪些参数? - thecountofzero
@thecountofzero $("#yourFlotDiv").off("yourEventHandlerTORemove"); 更多信息请参见 http://api.jquery.com/off/ - Ravi Gadag
我在谈论确保 flot 清除所有事件处理程序并删除其创建的所有 DOM 元素。 - thecountofzero

0

要移除flot图表

var placeholder = $("#FlotDiv");
placeholder.unbind(); //Remove a previously-attached event handler from the elements.
placeholder.empty();

如果您想要解除绑定特定事件,可以使用以下方法:

$( "#foo").unbind( "click" );

欲知详情请查看this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接