Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Solution that waits for new quotes

P

@GregLyons

Desktop design screenshot for the Advice generator app coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
  • API
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


I tried to implement a feature where the button will be disabled for 2 seconds after fetching (since that's how the Advice Slip API works; it returns a cached version of the previous quote if you request too frequently). It seems to work decently, but I've noticed that rapidly clicking on it can break it. Is this something you run into, and if so, how would you handle it?

Community feedback

@lipe11

Posted

hi,

I think the timeout worked pretty good. I tried to fast click the button but barely saw it act weirdly.

However, if you want to get rid of the effect, you might want to try something like throttling. I'll leave a reference here. As a first approach, you could try something like this

let waiting = false

buttonWrapper.addEventListener("click", (e) => {
  if (waiting) return
  waiting = true
  getAdvice()
  setTimeout(() => {
    waiting = false
  }, 2000);
})

Marked as helpful

1

P

@GregLyons

Posted

@lipe11 Thanks for pointing that out. I've applied throttling in a previous project when making calls to an API, but I didn't know it well enough to realize that I should apply it here as well. The video was helpful, too!

I realize now the issue: I was putting removal for the "--disabled" class on a 2-second timeout, but I was allowing such a timeout to be set on every click of the button, even when it was disabled. So if I clicked on the button rapidly, say:

  1. once, (when the button is enabled, which disables the button)
  2. again, one second later (while the button is still disabled)
  3. again, two seconds later (when it becomes enabled again),

then the timeout from step 2 would enable the button at 3 seconds (only one second after step 3), rather than at 4 seconds (two seconds after step 3, as intended). Your suggestion is precisely the way to avoid this.

0
Ivan 2,630

@isprutfromua

Posted

also i found some css issues: use only one type of selectors (class or id, but class is preferred)

.card__text {

 ....
#card-button {

it's better to set class for an image

#advice-button > img {
  display: block;
}

it's not good to use nesting selectors with bem methodology

.card__wrapper > .card__header {

Marked as helpful

1

P

@GregLyons

Posted

@isprutfromua

I'll keep these BEM pointers in mind for the future, thanks!

0
Ivan 2,630

@isprutfromua

Posted

@GregLyons I am glad that my help was useful to you

Cheers, peace and happy coding!

0
Ivan 2,630

@isprutfromua

Posted

Hi there. You did a good job 😎

🛠️

keep improving your programming skills

your solution looks great, however, if you want to improve it, you can follow these steps:

✅ you can do it with HTML picture

function changeDivider() {
  const dividerImg = document.getElementById('divider-img');

  if (window.innerWidth > 500) {
    dividerImg.src = 'images/pattern-divider-desktop.svg';
  }
  else {
    dividerImg.src = 'images/pattern-divider-mobile.svg';
  }
}

✅ there is no need to prevent event inside the fetching function

getAdvice(e) {
  e.preventDefault();

Marked as helpful

1

P

@GregLyons

Posted

@isprutfromua

I didn't know about the <picture> element, thanks! I can think of a lot of challenges where I should've used that...

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord