javascript / d3 - 将参数传递给函数 [英] javascript / d3 - passing parameters to a function

查看:228
本文介绍了javascript / d3 - 将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个名为d3的javascript库,以便对一个对象执行一些动画。在每个动画的结尾,我想调用一个函数,它从调用动画的对象传递数据,但它不工作。如何完成这个?这是我的代码:

I'm using a javascript library called d3 in order to perform some animations on an object. At the end of each animation I want to call a function which is passed data from the object which called the animation, but it's not working. How do I accomplish this? Here's my code:

function selectArcs() {
    d3.selectAll("g.arc > path")
        .each(arcTween)
}


function arcTween(d,i){
    console.log(i); //registers as 0, then 1

    d3.select(this)
        .transition().duration(1000)
        .attrTween("d", tweenArc({ init : d.value }))
        .each("end",function(i){ console.log(i); }); //registers as 0, then 0 - should be 0, then 1
}


推荐答案

function(i){console.log(i); } 你正在调用参数 i ,但实际上通常被称为 d

In function(i){ console.log(i); } You're calling the parameter i but it's actually what would commonly be called d!

尝试用 function(dOrWhatever){console.log(i); } (这应该从函数arcTween(d,i)获得 i

Try replacing it with function(dOrWhatever){ console.log(i); } (this should get the i variable from function arcTween(d,i))

这篇关于javascript / d3 - 将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆