admin管理员组

文章数量:1323529

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

You 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