【发布时间】:2016-06-25 00:42:21
【问题描述】:
假设我在函数中使用 d3.xml() 将 SVG 资源加载到浏览器中。如果我想在资产加载到内存后调用回调函数(“cb”),我该怎么做?
作为示例代码,我尝试了以下方法:
var svg_xml = null;
var load_svg = function(svg_path, cb) {
d3.xml(svg_path, "image/svg+xml", function(error, xml) {
if (error) throw error;
svg_xml = xml;
if (cb) cb;
});
};
然后在脚本的其他地方测试回调:
load_svg("/foo/bar/baz.svg", function() {
console.log("baz.svg loaded!");
});
SVG 最终被加载到内存中。但是,不会调用回调 - 没有日志消息结果。
load_svg 内的cb 的范围是否存在问题,导致d3.xml() 不知道cb 是什么?
【问题讨论】:
标签: javascript d3.js scope callback