setInterval()

Steps to reproduce:title of the document should be changing as new messages every second. But it doesnt happen here ,only on the initial refresh after 1 sec the title changes and remains num
setInterval(() =>
document.title = '(2) New Messages', 1000)
setInterval(() =>
document.title = '(2) New Messages', 1000)
82 Replies
Vandana
Vandana2mo ago
Can anyone help me out with this.
Chris Bolson
Chris Bolson2mo ago
At first glance that should work. Presumably you are changing the value of "2" otherwise you won't see any change. As a quick test, this works, updating the title with a random number every second:
function random(){
return randomNumber = Math.floor(Math.random() * 100);
}
setInterval(() => document.title = `(${random()}) New Messages`,
1000);
function random(){
return randomNumber = Math.floor(Math.random() * 100);
}
setInterval(() => document.title = `(${random()}) New Messages`,
1000);
Sanan
Sanan2mo ago
I think closure is causing issue- You might want to try the code below; it works function startUpdatingTitle() { let messageCount = 0; setInterval(()=> { messageCount += 1; document.title = (${messageCount}) New Messages; console.log(document.title); }, 1000); } startUpdatingTitle();
Chris Bolson
Chris Bolson2mo ago
This may not be relevant but it is also worth mentioning that this won't visibly work if you are testing this on a Codepen or similar. This is because the output is being displayed in an iFrame so the document.title being updated will not be the global title, just the iframe contents title.
glutonium
glutonium2mo ago
works fine for me
Choo♚𝕂𝕚𝕟𝕘
Why are you using setInterval instead of setTimeout? After the first change, all subsequent runs will set the same title, so you won't see anything else happen.
Vandana
Vandana2mo ago
Why it doesnt change the static data though there is an interval it has to keep it blinking.It blinks only for the refresh then remains num setTimeout occurs only once, but the requirement here is for every interval.
Chris Bolson
Chris Bolson2mo ago
Why would it blink? That code will just update the value every second, not make it blink.
Choo♚𝕂𝕚𝕟𝕘
But every interval sets the exact same text. The code shared by Chris and Sanan update with different text, but your original code just updates with the exact same thing, so you won't see anything change.
Chris Bolson
Chris Bolson2mo ago
You would have to set it to be empty before setting the message. You could do that by including an setTimeout within the setInterval
setInterval(() => {
document.title = ''
setTimeout(() => document.title = '(2) New Messages',500)

}, 1000)
setInterval(() => {
document.title = ''
setTimeout(() => document.title = '(2) New Messages',500)

}, 1000)
But even then it wont actually be empty as the browser will give it the default title whilst it is "empty".
Choo♚𝕂𝕚𝕟𝕘
If you want a blink, you have to alternate between an empty string and the text that you want to be blinking.
Chris Bolson
Chris Bolson2mo ago
Maybe something like this will get closer :
setInterval(() => {
document.title = '( ) New Messages'
setTimeout(() => document.title = '(2) New Messages',500)

}, 1000)
setInterval(() => {
document.title = '( ) New Messages'
setTimeout(() => document.title = '(2) New Messages',500)

}, 1000)
But the empty brackets will cause a slight jump between content as the "2" will take up more space. (I'm not convinced that any of this is a good idea though. I personally. would find it very annoying)
Vandana
Vandana2mo ago
yea this means a toggle between browsers title and our title so you mean with the same text we cant see that interval changeing the same text again and again.
Chris Bolson
Chris Bolson2mo ago
If you could put the number at the end you would avoid the "jump" which might make it more acceptable
setInterval(() => {
document.title = 'New Messages'
setTimeout(() => document.title = 'New Messages (2)',500)

}, 1000)
setInterval(() => {
document.title = 'New Messages'
setTimeout(() => document.title = 'New Messages (2)',500)

}, 1000)
Exactly. It is just like updating the DOM, unless you add a timeout of some sort, the change is instant.
Chris Bolson
Chris Bolson2mo ago
Choo♚𝕂𝕚𝕟𝕘
You must alternate with an empty or partial string to see it blink. Alternating a value with an identical value doesn't look like anything happened. I don't know why you expect a moment of blankness when there is a change. That is not something built into the system.
Vandana
Vandana2mo ago
its not blink actually its like on/off
let intervalId = setInterval(function () {
document.title = `${Math.floor(Math.random)}New messages`, 1000)
let intervalId = setInterval(function () {
document.title = `${Math.floor(Math.random)}New messages`, 1000)
This works because there is a change in title every interval.
let intervalId = setInterval(function () {
document.title = `(20)New messages`, 1000)
let intervalId = setInterval(function () {
document.title = `(20)New messages`, 1000)
Mean while this doesnt have any effect the code wont update the title every second yea we are updating the DOM only in 1sec .change is instant you mean we cant see the static text change?
ἔρως
ἔρως2mo ago
i will be the downer here: how about you ... don't blink the number? it's annoying, irritating, distracting and the user can't hide it if something is asking for my attention, it better be very important with that said, it is working: every second you're setting it to the same exact string
Vandana
Vandana2mo ago
let intervalId = setInterval(function () {
document.title = `(20)New messages`, 1000)
let intervalId = setInterval(function () {
document.title = `(20)New messages`, 1000)
you are saying this is working?
ἔρως
ἔρως2mo ago
yes, it is
Vandana
Vandana2mo ago
nopes
ἔρως
ἔρως2mo ago
if you close the blackets, it works
Vandana
Vandana2mo ago
No i have closed it .it works only for the refresh and then remains static i cant see the updation
ἔρως
ἔρως2mo ago
because ... you're always changing to the same value every second, it's the same string not a different one
Vandana
Vandana2mo ago
Yes that was the discussion above.
ἔρως
ἔρως2mo ago
then change to a different string
Vandana
Vandana2mo ago
is that the thumb rule ? that if anything is same we cant see in the DOM UPDATION i want every second the same string should show that it has updated in the DOM. which is not possible
ἔρως
ἔρως2mo ago
let show_number = false;
let number = 20;
let title = 'Nice title!'

let interval = setInterval(function() {
if(show_number) {
document.title = `(${number}) ${title}`;
} else {
document.title = title;
}

show_number != show_number;
}, 1000);
let show_number = false;
let number = 20;
let title = 'Nice title!'

let interval = setInterval(function() {
if(show_number) {
document.title = `(${number}) ${title}`;
} else {
document.title = title;
}

show_number != show_number;
}, 1000);
this should do it it is, but if you change from the same to the same, you won't see a difference stand up, and walk 0 steps forward and then 0 steps back tell me how many times you've walked
Vandana
Vandana2mo ago
Only standing is the task here nothing can be done apart from this.
ἔρως
ἔρως2mo ago
no, you're saying "change to the string a" and then you say "change to the string a" and you then say "change to the string a" guess what? you're going to get a what did you expect? potatoes? a tv? if you always set the same value, you always see the same value only you have to set a different value to see a difference
Vandana
Vandana2mo ago
yea that trigger or changing point i should see na Okay okay your example has has change in title
ἔρως
ἔρως2mo ago
my code has a bug
let show_number = false;
let number = 20;
let title = 'Nice title!'

let interval = setInterval(function() {
if(show_number) {
document.title = `(${number}) ${title}`;
} else {
document.title = title;
}

show_number = !show_number;
}, 1000);
let show_number = false;
let number = 20;
let title = 'Nice title!'

let interval = setInterval(function() {
if(show_number) {
document.title = `(${number}) ${title}`;
} else {
document.title = title;
}

show_number = !show_number;
}, 1000);
this should work
ἔρως
ἔρως2mo ago
No description
No description
ἔρως
ἔρως2mo ago
yes, i opened stackoverflow because it wasn't working, and i had to triple check for sanity this is the equivalent of typing 1 + 1 in your calculator >.>
Vandana
Vandana2mo ago
means?
ἔρως
ἔρως2mo ago
have you ever typed something super basic, like 2+3 in the calculator?
Vandana
Vandana2mo ago
ofcourse why?
ἔρως
ἔρως2mo ago
that's what i did, and im defending myself before you judge me >.>
Vandana
Vandana2mo ago
hey i didnt judge you infact i appreciate all of your efforts.
ἔρως
ἔρως2mo ago
you know what i mean but hey, it works, and it is just as irritatingly annoyingly annoying as i imagined
Vandana
Vandana2mo ago
as of now i know only this seriously i am not getting what are you talking about sanity and all i am confused here as well
ἔρως
ἔρως2mo ago
i just meant that my first example had a bug, and i was checking why it didn't work and i opened stackoverflow to be sure that i was doing it right then i used the page to test the code and get it to work
Vandana
Vandana2mo ago
AHH sanity means you are saying checking 100 times that 1+1 is 2 or not
ἔρως
ἔρως2mo ago
yes, because im getting 3, so, something wasn't adding up (by "3" im mean that i was getting an unexpected result)
Vandana
Vandana2mo ago
Great did you look over stackoverflow for the logic or you coded on your own ?
ἔρως
ἔρως2mo ago
i looked how to set a title on the document
ἔρως
ἔρως2mo ago
Stack Overflow
How to dynamically change a web page's title?
I have a webpage that implements a set of tabs each showing different content. The tab clicks do not refresh the page but hide/unhide contents at the client side. Now there is a requirement to cha...
ἔρως
ἔρως2mo ago
this, it is what's in the browser
Vandana
Vandana2mo ago
Thats great. Thank you one and all for the explaination. It was really a great help
ἔρως
ἔρως2mo ago
you're welcome do you understand my code?
Vandana
Vandana2mo ago
Thats why i asked how did you write in a simple way
ἔρως
ἔρως2mo ago
well, it is nothing extraordinary i just use a boolean that im changing between true and false, and it decides if it shows the value or not
Vandana
Vandana2mo ago
i always miss to write the boolean it wont flash to me
ἔρως
ἔρως2mo ago
if it is to show the number, it just uses a template string yes, which is why i said that you're changing into the same value, always
Vandana
Vandana2mo ago
do you have any basic use cases to practice for this kind of stuff.
ἔρως
ἔρως2mo ago
nope it's just logic and experience and planning, but i plan it all in my head but you should learn a tiny bit of software engineering and learn how to do basic system analysis and how to make a data flux diagram and also learn how to think in terms of splitting a task for an algorithm
Vandana
Vandana2mo ago
yes agreed underated skill
ἔρως
ἔρως2mo ago
it's sorely lacking
Vandana
Vandana2mo ago
yea
ἔρως
ἔρως2mo ago
most people that go to school don't learn anything related to this
Vandana
Vandana2mo ago
trying to improve each day
ἔρως
ἔρως2mo ago
so, you show them a new problem and they crumble like a spaghetti stick string? you know what i mean
Vandana
Vandana2mo ago
?
ἔρως
ἔρως2mo ago
you know, spaghetti isn't that strong any pressure and it breaks
Vandana
Vandana2mo ago
talking about myself?
ἔρως
ἔρως2mo ago
no, fresh developers that come out of school
Vandana
Vandana2mo ago
yea to make pasta
ἔρως
ἔρως2mo ago
yeah, but now imagine someone who's developer skills are like that string of pasta
Vandana
Vandana2mo ago
Including me
ἔρως
ἔρως2mo ago
if it's laying on the table, it handles pressure okay if you change the environment and just hang it a bit outside the confort of the table, it crumbles into nothing in moments
Vandana
Vandana2mo ago
yEs pasta sticks
ἔρως
ἔρως2mo ago
yes, that's what i meant it doesn't mean i don't need help but if i need help, it means im in serious serious trouble and what i need help with is absolutely niche or absolutely basic and idiotic
Vandana
Vandana2mo ago
So overall you are saying if the problem or a feature is given to you, you will analayze in your brain how to do it.Then you write the logic on your own and here if you need help it will be kind of related to basics only.
ἔρως
ἔρως2mo ago
mostly that, yes
Vandana
Vandana2mo ago
what if that feature is new?
ἔρως
ἔρως2mo ago
all features are new
Vandana
Vandana2mo ago
Initial algorithm/analyze it in your brain would be difficult right? to start with that feature itself would be like pasta sticks because we wont be having any idea about it like how to do it and all
ἔρως
ἔρως2mo ago
not at all in this case, it is something really simple but something more complicated, i may do it on paper or in excalidraw
Vandana
Vandana2mo ago
meaning in this post you are talking about,SetInterval. I was asking about new features how would you approach.
ἔρως
ἔρως2mo ago
i would need to know the system first, in and out and everything and then i would need to know more about the feature: what's expected, what i have to put and where, how it will look like, how it should work, any specific functionality for this feature... a lot of things
Vandana
Vandana2mo ago
And then? System Design?
ἔρως
ἔρως2mo ago
and then, i plan on how i will implement everything and implement it
Want results from more Discord servers?
Add your server