仅针对特定的setSignal运行createEffect,以触发用户交互
我正在尝试构建一个文本编辑器,其中_cursor
是光标的位置。 setInterval 4000秒
就像用户移动光标。在这种情况下,我想运行效果。
但是通常我将光标设置为内部效果,例如,当某些文本被删除或添加或撤消时。在这种情况下,我不想在示例中运行createefect
。
因此,如何使createefect
仅用于第二个setInterval 4000
而不是第一个set> setInterval
。
import { render } from "solid-js/web";
import { For, createSignal, createEffect, on } from "solid-js";
function Counter() {
const [_cursor, _setCursor] = createSignal(100)
createEffect(on(_cursor, (v, p) => {
if (p && v < p) {
console.log('reset')
}
}))
setInterval(() => {
_setCursor(_cursor() - 1)
}, 500)
setInterval(() => {
_setCursor(_cursor() - 1)
}, 4000)
return (<>
<span>{_cursor()}</span>
</>)
}
render(() => <Counter />, document.getElementById("app"));
I am trying to build a text editor, where _cursor
is position of the cursor. setInterval 4000 seconds
is like user moving the cursor. In which case I want to run an effect.
But often I set the cursor as a side effect internally, for example when some text is deleted or added or an undo. In that case I don't want to run the createEffect
in the example.
So how do I make the createEffect
only run for second setInterval 4000
and not for the first setInterval
.
import { render } from "solid-js/web";
import { For, createSignal, createEffect, on } from "solid-js";
function Counter() {
const [_cursor, _setCursor] = createSignal(100)
createEffect(on(_cursor, (v, p) => {
if (p && v < p) {
console.log('reset')
}
}))
setInterval(() => {
_setCursor(_cursor() - 1)
}, 500)
setInterval(() => {
_setCursor(_cursor() - 1)
}, 4000)
return (<>
<span>{_cursor()}</span>
</>)
}
render(() => <Counter />, document.getElementById("app"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置另一个标志并使用其值来控制效果的执行:
请注意,效果不会跟踪标志:
You can set another flag and use its value to control the execution of the effect:
Please note that, the effect does not track the flag: