每次值变化时运行一个方法
我想知道你们中是否有人可以帮助我解决我认为的观察问题。
我有一个元素(更具体地说是 svg),每次某处的值发生更改时我都想更新它。
我有变量: GetThreadTree().treeBoxObject.getFirstVisibleRow()
最初为 0。我想在每次 GetThreadTree().treeBoxObject 的值时运行一个
函数 updateCanvas()
。 getFirstVisibleRow() 发生变化。
我所拥有的是:
canvas.observe(GetThreadTree().treeBoxObject.getFirstVisibleRow(), "scroll", updateCanvas());
但它仅在第一次调用时调用 updateCanvas()
一次,并且由于某种原因不执行其后面的代码。 我检查了错误控制台,什么也没有。
有任何想法吗?
I wonder if any of you guys can help me with what I think is observing problem.
I have an element (svg to be more specific) that I want to update every time a value somewhere is changed.
I have variable:GetThreadTree().treeBoxObject.getFirstVisibleRow()
that initially is 0. I want to run a function updateCanvas()
every time value of GetThreadTree().treeBoxObject.getFirstVisibleRow()
changes.
What I have is:
canvas.observe(GetThreadTree().treeBoxObject.getFirstVisibleRow(), "scroll", updateCanvas());
But it calls updateCanvas()
only once, when it's called for the first time, and for some reason does not execute the code that is after it. I checked error console and nothing is there.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的逻辑完全错了地方。 当您更新值时,无论什么都需要通过函数在一个地方完成,例如 treeBoxObject.IncrementRow() 或类似的函数。
然后您可以让该函数触发一个事件,例如 onTreeBoxRowIncremented。 该事件是您监听的事件,当该事件发生变化时,您可以进行检查并更新您喜欢的任何内容。
请原谅奇怪的函数名称只是试图使用你所拥有的。
You logic is all in the wrong place. When you update your value what ever it is that needs to be done in one place by a function, something like treeBoxObject.IncrementRow() or similar.
Then you can have that function fire an event, like onTreeBoxRowIncremented. That event is what you listen out for, when that changes then you can do your check and update whatever you like.
Excuse the weird function names just trying to use what you have.
解决这个问题的一种方法是:
在调用 checkRow 之前:
但这不是最优雅的解决方案,但它有效;)
One way of solving this problem is:
And before checkRow is called:
But it's not the most elegant solution but it works ;)