KEY NOT FOUND: coverImgAlt
Logo
See All Photos
Best Places to Work 2022

Meta

Engaged Employer

Meta

Add an Interview

Interview Question

Front End Engineer Interview

-Waterloo, ON

Meta

Related to question 1, how could you implement debouncing? Say you wanted the `handleScroll` function to be called only after 200ms has passed between scroll events.

AnswerAdd Tags

Interview Answers

4 Answers

3

const debounce = (callback, time) => { let timeout = null; return () => { clearTimeout(timeout); timeout = setTimeout[() => { callback(); }, time); } } const logger = (e) => { console.log('server called with'); } window.addEventListener('scroll', debounce(logger, 400));

ela pou sai on

2

This should really called as throttling instead of deboucing.

Anonymous on

1

You should use keep track of the time at which the previous scroll was called. var recentScrollTime = 0; var currentScrollEvt = undefined; delayedHandleScroll(Date.now()) delayedHandleScroll = (timeCalled) => { RecentScrollTime = timeCalled; if (timeCalled - recentScrollTime > 200) { ClearTimeout(currentScrollEvt) // check if currentScroll isn’t undefined first though handleScrollFn() } else { if (currentScrollEvent) { ClearTimeout(currentScrollEvt); } // Set the scroll event to fired after the 200ms // this solves the case when the next scroll was called less than 200ms after without dismissing it completely currentScrollEvt = setTimeout[handleScrollFn, timeCalled-recentScrollTime) } }

Anonymous on

2

(function waitHandleScroll(){ handleScroll(); setTimeout[waitHandleScroll, 200); })();

Osvaldo on

Add Answers or Comments

To comment on this, Sign In or Sign Up.

Meta Careers

Cover image for Meta

Meta builds technologies that help people connect, find communities, and grow businesses. When Facebook launched in 2004, it changed the...More

  • Our Mission
  • Diversity & Inclusion
  • Meta Careers
  • Our Principles
This is the employer's chance to tell you why you should work for them. The information provided is from their perspective.