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

234
Visualizações
How to access a variable value that is defined inside another function JS

I have a variable totalActivatedOffers which is defined inside a function, inside an IF statement. I need to be able to use the value of this variable outside of the IF statement by another function but can't get it to work, it keeps returning undefined.

I've declared it at the top of the script block like this:

var totalActivatedOffers;

But still no joy. Here is an example of how the value is assigned to it (simplified for illustration):

if ("SSO" in sessionStorage) {
  setTimeout(function(){
    if (firstArrayLength.length == 3) {
      var totalActivatedSparksOffers = 4;
    }
    if (secondArrayLength.length == 3) {
      var totalActivatedRewardsOffers = 7;
    }
    var totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
  }, 2500) 
} // closes if SSO is inside session storage

// need to access the value here, outside of the IF statement
console.log('Total activated offers to subtract is ' + totalActivatedOffers);

Any help would be greatly appreciated.

SOLUTION:

Managed to get around the issue by making it a window variable like this:

window.totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Managed to solve the issue by making it a window variable:

window.totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
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