Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
How to add a like to something but only once using local storage?

I'm working with an API and i need to display movies and like them but i need to like them once. I tried to add a boolean and I had something like this:

let beenLiked = false;

const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
    console.log("ok like")
    axios.patch(`myUrl`)
    beenLiked = true
}, true)

the problem is when I like a movie, i can't like another one because of my boolean, how can I modify it to solve my problem?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you can get the name of the movie that is supposed to be liked (I don't have your html file to do it for you), you can track the names and do it like this:

let beenLiked = []

const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
    const movie = "The Movie" // Get the movie name or id which has to be liked
    if(beenLiked.indexOf(movie) === -1){
        console.log("ok like")
        axios.patch(`myUrl`)
        beenLiked.push(movie)
        return true
    }
    console.log("Already liked once")
    return false
})

here the simplified version without html:

let beenLiked = []

function a() {
  const movie = "The Movie" // Get the movie the likeButton is attached to
  if (beenLiked.indexOf(movie) === -1) {
    beenLiked.push(movie)
    return true
  }
  return false
}
console.log(a())
console.log(a())

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda