admin管理员组文章数量:1323185
I am have tried to use the once function in lodash as follows:
_.once(pageTwoSegmentEvent);
_.once(() => {
pageTwoSegmentEvent();
})
I have also tried importing only the once function from lodash and not the _
once(pageTwoSegmentEvent);
once(() => {
pageTwoSegmentEvent();
});
But the function pageTwoSegmentEvent never actually gets called. However, if I remove the once function that its wrapped in and just call it like normal then it works but gets called too many times. Does anyone know how to get the once function from lodash to work?
I am have tried to use the once function in lodash as follows:
_.once(pageTwoSegmentEvent);
_.once(() => {
pageTwoSegmentEvent();
})
I have also tried importing only the once function from lodash and not the _
once(pageTwoSegmentEvent);
once(() => {
pageTwoSegmentEvent();
});
But the function pageTwoSegmentEvent never actually gets called. However, if I remove the once function that its wrapped in and just call it like normal then it works but gets called too many times. Does anyone know how to get the once function from lodash to work?
Share Improve this question asked Jun 6, 2019 at 18:42 FairyQueenFairyQueen 2,3738 gold badges38 silver badges58 bronze badges2 Answers
Reset to default 6You don't use _.once
like that. You use the new function that _.once
returns.
function foo() {
console.log("foo");
}
const oncedFoo = _.once(foo);
oncedFoo();
oncedFoo();
oncedFoo();
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>
_.once
returns a function, so you have to call the returned function
Try doing
_.once(pageTwoSegmentEvent)();
(note the parenthesis at the end)
本文标签: javascriptHow to use the once() function in lodashStack Overflow
版权声明:本文标题:javascript - How to use the _.once() function in lodash? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130047a2422116.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论