Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Variable not being updated and function duplicating

First of all thanks for any future help!

I am creating a notification script that modifies the title tab to make the (1) appear. I am pulling the number of alerts from a notify_count variable updated by the website.

The situation I am facing is that:

1.The (1) appears in tab but is duplicated everytime the newUpdate interval executes.

ex: (1) my site, then (1) (1) my site, etc...

  1. If notify_count value increases, the changeTitle function doesn't update numb and the same number appears in the tab.

ex: notify_count = 1 -> (1) my site -> notify_count = 2 -> (1) my site

var numb = notify_count.toString();


function changeTitle() {
   
    var newTitle = '(' + numb + ') ' + document.title;
    document.title = newTitle; }

function newUpdate() {
   var update = setInterval(changeTitle, 2000);

}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to check if the title already begins with (notify_count). If it does, you have to replace that instead of adding the count again at the beginning.

function changeTitle() {
  let prefix = `(${notify_count})`;
  if (document.title.match(/^\(\d+)\)/) {
      document.title = document.title.replace(/^\(\d+)\)/, prefix);
    } else {
      document.title = `${prefix} ${document.title}`;
    }
  }
}

function newUpdate() {
  var update = setInterval(changeTitle, 2000);
}

Another option is to save the original title in another variable when the page loads.

let original_title = document.title;

function changeTitle() {
  document.title = `(${notify_count}) ${original_title}`;
}

function newUpdate() {
  var update = setInterval(changeTitle, 2000);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are getting a duplicate title e.g.(1) (1) my site

because of this;

var newTitle = '(' + numb + ') ' + document.title;
document.title = newTitle;

You are setting document.title to newTitle's value but you are using the existing document.title value in defining newTitle. In essence this line is appending "(num)" to the document.title again and again and again.

The first time through document.title is "My Site" before you append the "(num)". Then the next time through document.title is "(1) My Site", which is then used to define newTitle to be "(1) (1) My Site". etc...

To correct this you can use a RegEx in changeTitle() to replace only the "(num)" portion of the document.title with String.Replace() or you can define a new var at the top to hold the original document.title and use that var as the base from which to build your newTitle

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda