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
VandanaOP3mo ago
Can anyone help me out with this.
Chris Bolson
Chris Bolson3mo 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
Sanan3mo 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 Bolson3mo 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
glutonium3mo 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
VandanaOP3mo 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 Bolson3mo 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 Bolson3mo 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 Bolson3mo 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
VandanaOP3mo 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 Bolson3mo 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 Bolson3mo 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
VandanaOP3mo 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?
ἔρως
ἔρως3mo 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
VandanaOP3mo 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?
ἔρως
ἔρως3mo ago
yes, it is
Vandana
VandanaOP3mo ago
nopes
ἔρως
ἔρως3mo ago
if you close the blackets, it works
Vandana
VandanaOP3mo ago
No i have closed it .it works only for the refresh and then remains static i cant see the updation
ἔρως
ἔρως3mo ago
because ... you're always changing to the same value every second, it's the same string not a different one
Vandana
VandanaOP3mo ago
Yes that was the discussion above.
ἔρως
ἔρως3mo ago
then change to a different string
Vandana
VandanaOP3mo 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
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
Only standing is the task here nothing can be done apart from this.
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
yea that trigger or changing point i should see na Okay okay your example has has change in title
ἔρως
ἔρως3mo 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
ἔρως
ἔρως3mo ago
No description
No description
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
means?
ἔρως
ἔρως3mo ago
have you ever typed something super basic, like 2+3 in the calculator?
Vandana
VandanaOP3mo ago
ofcourse why?
ἔρως
ἔρως3mo ago
that's what i did, and im defending myself before you judge me >.>
Vandana
VandanaOP3mo ago
hey i didnt judge you infact i appreciate all of your efforts.
ἔρως
ἔρως3mo ago
you know what i mean but hey, it works, and it is just as irritatingly annoyingly annoying as i imagined
Vandana
VandanaOP3mo 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
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
AHH sanity means you are saying checking 100 times that 1+1 is 2 or not
ἔρως
ἔρως3mo ago
yes, because im getting 3, so, something wasn't adding up (by "3" im mean that i was getting an unexpected result)
Vandana
VandanaOP3mo ago
Great did you look over stackoverflow for the logic or you coded on your own ?
ἔρως
ἔρως3mo ago
i looked how to set a title on the document
ἔρως
ἔρως3mo 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...
ἔρως
ἔρως3mo ago
this, it is what's in the browser
Vandana
VandanaOP3mo ago
Thats great. Thank you one and all for the explaination. It was really a great help
ἔρως
ἔρως3mo ago
you're welcome do you understand my code?
Vandana
VandanaOP3mo ago
Thats why i asked how did you write in a simple way
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
i always miss to write the boolean it wont flash to me
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
do you have any basic use cases to practice for this kind of stuff.
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
yes agreed underated skill
ἔρως
ἔρως3mo ago
it's sorely lacking
Vandana
VandanaOP3mo ago
yea
ἔρως
ἔρως3mo ago
most people that go to school don't learn anything related to this
Vandana
VandanaOP3mo ago
trying to improve each day
ἔρως
ἔρως3mo ago
so, you show them a new problem and they crumble like a spaghetti stick string? you know what i mean
Vandana
VandanaOP3mo ago
?
ἔρως
ἔρως3mo ago
you know, spaghetti isn't that strong any pressure and it breaks
Vandana
VandanaOP3mo ago
talking about myself?
ἔρως
ἔρως3mo ago
no, fresh developers that come out of school
Vandana
VandanaOP3mo ago
yea to make pasta
ἔρως
ἔρως3mo ago
yeah, but now imagine someone who's developer skills are like that string of pasta
Vandana
VandanaOP3mo ago
Including me
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
yEs pasta sticks
ἔρως
ἔρως3mo 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
VandanaOP3mo 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.
ἔρως
ἔρως3mo ago
mostly that, yes
Vandana
VandanaOP3mo ago
what if that feature is new?
ἔρως
ἔρως3mo ago
all features are new
Vandana
VandanaOP3mo 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
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
meaning in this post you are talking about,SetInterval. I was asking about new features how would you approach.
ἔρως
ἔρως3mo 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
VandanaOP3mo ago
And then? System Design?
ἔρως
ἔρως3mo ago
and then, i plan on how i will implement everything and implement it
Want results from more Discord servers?
Add your server