Javascript double timer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
So i want to set a vanilla JavaScript to display a message screen text for 30-40 seconds then display a different text for 10 seconds. The first message should change in particular order (ie.. hello, world, i ,love java, script). And cycle through. I tried to put it in an a array with a timer but no luck can't get the timer to oscillate between the 30 and 10 seconds.
So ie... hello for 30 sec, then world for 10 sec, then i love for 30 sec and so on.
Currently I'm following this example but I'd rather not do the math there's got to be a cleaner better way.
https://www.w3schools.com/jsref/met_win_settimeout.asp
This what i am doing now. Abbreviated
function timedText() {
setTimeout(myTimeout1, 10000) setTimeout(myTimeout2, 30000) setTimeout(myTimeout3, 40000)
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
javascript arrays loops settimeout
add a comment |
So i want to set a vanilla JavaScript to display a message screen text for 30-40 seconds then display a different text for 10 seconds. The first message should change in particular order (ie.. hello, world, i ,love java, script). And cycle through. I tried to put it in an a array with a timer but no luck can't get the timer to oscillate between the 30 and 10 seconds.
So ie... hello for 30 sec, then world for 10 sec, then i love for 30 sec and so on.
Currently I'm following this example but I'd rather not do the math there's got to be a cleaner better way.
https://www.w3schools.com/jsref/met_win_settimeout.asp
This what i am doing now. Abbreviated
function timedText() {
setTimeout(myTimeout1, 10000) setTimeout(myTimeout2, 30000) setTimeout(myTimeout3, 40000)
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
javascript arrays loops settimeout
add a comment |
So i want to set a vanilla JavaScript to display a message screen text for 30-40 seconds then display a different text for 10 seconds. The first message should change in particular order (ie.. hello, world, i ,love java, script). And cycle through. I tried to put it in an a array with a timer but no luck can't get the timer to oscillate between the 30 and 10 seconds.
So ie... hello for 30 sec, then world for 10 sec, then i love for 30 sec and so on.
Currently I'm following this example but I'd rather not do the math there's got to be a cleaner better way.
https://www.w3schools.com/jsref/met_win_settimeout.asp
This what i am doing now. Abbreviated
function timedText() {
setTimeout(myTimeout1, 10000) setTimeout(myTimeout2, 30000) setTimeout(myTimeout3, 40000)
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
javascript arrays loops settimeout
So i want to set a vanilla JavaScript to display a message screen text for 30-40 seconds then display a different text for 10 seconds. The first message should change in particular order (ie.. hello, world, i ,love java, script). And cycle through. I tried to put it in an a array with a timer but no luck can't get the timer to oscillate between the 30 and 10 seconds.
So ie... hello for 30 sec, then world for 10 sec, then i love for 30 sec and so on.
Currently I'm following this example but I'd rather not do the math there's got to be a cleaner better way.
https://www.w3schools.com/jsref/met_win_settimeout.asp
This what i am doing now. Abbreviated
function timedText() {
setTimeout(myTimeout1, 10000) setTimeout(myTimeout2, 30000) setTimeout(myTimeout3, 40000)
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
javascript arrays loops settimeout
javascript arrays loops settimeout
edited Nov 22 '18 at 7:16
Dhaval Pankhaniya
1,6231023
1,6231023
asked Nov 22 '18 at 6:25
pablopablo
104
104
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Why not just have two functions, one that sets the other and vice versa?
function one() {
window.alert('Messsage One')
setTimeout(two, 10*1000);
}
function two() {
window.alert('Message two');
setTimeout(one, 30*1000);
}
setTimeout(two, 30*1000);
Obviously, you could change the window.alert
calls to whatever method you're actually using for displaying the text.
EDIT
Ok, to update based on your request. If you want an array of words to cycle, and varying times, I've done it this way. Given the HTML:
<div id='msgBox'></div>
You can setup an array and a function to display from that array like so:
var msgBox = document.getElementById('msgBox');
var messages = ['hello', 'world', 'i' ,'love', 'java', 'script']
function updateMessage() {
// take the first word out of the array and store it in the msg variable
var msg = messages.shift();
// set the div content to the word
msgBox.innerHTML = msg;
// pick a duration based on if the remaining array length is odd or even
var duration = messages.length % 2 ? 10 * 1000 : 30 * 1000
// queue up the next message update
setTimeout(updateMessage, duration)
}
setTimeout(updateMessage, 10*1000)
Obviously, you can tweak the duration bit however you want; I just did a modulo operation so that basically if the array length is even you wait 10 seconds if it's odd you wait 30 seconds.
Likewise if you want to update the inner content to include other information (e.g. like you had an h2 with various styles) you can either put that whole string in the array, or you can use something similar to my duration logic to select the correct wrapper.
Here's the fiddle
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
add a comment |
you can do something like below!
you can modify timeout interval as per your requirement
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understandThe only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?
– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
add a comment |
Best way to do it with OOP.
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
In case if you need to add other functionality of timer in future, you can easily add and remove it not needed anyone.
Hope this helps!
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424999%2fjavascript-double-timer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why not just have two functions, one that sets the other and vice versa?
function one() {
window.alert('Messsage One')
setTimeout(two, 10*1000);
}
function two() {
window.alert('Message two');
setTimeout(one, 30*1000);
}
setTimeout(two, 30*1000);
Obviously, you could change the window.alert
calls to whatever method you're actually using for displaying the text.
EDIT
Ok, to update based on your request. If you want an array of words to cycle, and varying times, I've done it this way. Given the HTML:
<div id='msgBox'></div>
You can setup an array and a function to display from that array like so:
var msgBox = document.getElementById('msgBox');
var messages = ['hello', 'world', 'i' ,'love', 'java', 'script']
function updateMessage() {
// take the first word out of the array and store it in the msg variable
var msg = messages.shift();
// set the div content to the word
msgBox.innerHTML = msg;
// pick a duration based on if the remaining array length is odd or even
var duration = messages.length % 2 ? 10 * 1000 : 30 * 1000
// queue up the next message update
setTimeout(updateMessage, duration)
}
setTimeout(updateMessage, 10*1000)
Obviously, you can tweak the duration bit however you want; I just did a modulo operation so that basically if the array length is even you wait 10 seconds if it's odd you wait 30 seconds.
Likewise if you want to update the inner content to include other information (e.g. like you had an h2 with various styles) you can either put that whole string in the array, or you can use something similar to my duration logic to select the correct wrapper.
Here's the fiddle
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
add a comment |
Why not just have two functions, one that sets the other and vice versa?
function one() {
window.alert('Messsage One')
setTimeout(two, 10*1000);
}
function two() {
window.alert('Message two');
setTimeout(one, 30*1000);
}
setTimeout(two, 30*1000);
Obviously, you could change the window.alert
calls to whatever method you're actually using for displaying the text.
EDIT
Ok, to update based on your request. If you want an array of words to cycle, and varying times, I've done it this way. Given the HTML:
<div id='msgBox'></div>
You can setup an array and a function to display from that array like so:
var msgBox = document.getElementById('msgBox');
var messages = ['hello', 'world', 'i' ,'love', 'java', 'script']
function updateMessage() {
// take the first word out of the array and store it in the msg variable
var msg = messages.shift();
// set the div content to the word
msgBox.innerHTML = msg;
// pick a duration based on if the remaining array length is odd or even
var duration = messages.length % 2 ? 10 * 1000 : 30 * 1000
// queue up the next message update
setTimeout(updateMessage, duration)
}
setTimeout(updateMessage, 10*1000)
Obviously, you can tweak the duration bit however you want; I just did a modulo operation so that basically if the array length is even you wait 10 seconds if it's odd you wait 30 seconds.
Likewise if you want to update the inner content to include other information (e.g. like you had an h2 with various styles) you can either put that whole string in the array, or you can use something similar to my duration logic to select the correct wrapper.
Here's the fiddle
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
add a comment |
Why not just have two functions, one that sets the other and vice versa?
function one() {
window.alert('Messsage One')
setTimeout(two, 10*1000);
}
function two() {
window.alert('Message two');
setTimeout(one, 30*1000);
}
setTimeout(two, 30*1000);
Obviously, you could change the window.alert
calls to whatever method you're actually using for displaying the text.
EDIT
Ok, to update based on your request. If you want an array of words to cycle, and varying times, I've done it this way. Given the HTML:
<div id='msgBox'></div>
You can setup an array and a function to display from that array like so:
var msgBox = document.getElementById('msgBox');
var messages = ['hello', 'world', 'i' ,'love', 'java', 'script']
function updateMessage() {
// take the first word out of the array and store it in the msg variable
var msg = messages.shift();
// set the div content to the word
msgBox.innerHTML = msg;
// pick a duration based on if the remaining array length is odd or even
var duration = messages.length % 2 ? 10 * 1000 : 30 * 1000
// queue up the next message update
setTimeout(updateMessage, duration)
}
setTimeout(updateMessage, 10*1000)
Obviously, you can tweak the duration bit however you want; I just did a modulo operation so that basically if the array length is even you wait 10 seconds if it's odd you wait 30 seconds.
Likewise if you want to update the inner content to include other information (e.g. like you had an h2 with various styles) you can either put that whole string in the array, or you can use something similar to my duration logic to select the correct wrapper.
Here's the fiddle
Why not just have two functions, one that sets the other and vice versa?
function one() {
window.alert('Messsage One')
setTimeout(two, 10*1000);
}
function two() {
window.alert('Message two');
setTimeout(one, 30*1000);
}
setTimeout(two, 30*1000);
Obviously, you could change the window.alert
calls to whatever method you're actually using for displaying the text.
EDIT
Ok, to update based on your request. If you want an array of words to cycle, and varying times, I've done it this way. Given the HTML:
<div id='msgBox'></div>
You can setup an array and a function to display from that array like so:
var msgBox = document.getElementById('msgBox');
var messages = ['hello', 'world', 'i' ,'love', 'java', 'script']
function updateMessage() {
// take the first word out of the array and store it in the msg variable
var msg = messages.shift();
// set the div content to the word
msgBox.innerHTML = msg;
// pick a duration based on if the remaining array length is odd or even
var duration = messages.length % 2 ? 10 * 1000 : 30 * 1000
// queue up the next message update
setTimeout(updateMessage, duration)
}
setTimeout(updateMessage, 10*1000)
Obviously, you can tweak the duration bit however you want; I just did a modulo operation so that basically if the array length is even you wait 10 seconds if it's odd you wait 30 seconds.
Likewise if you want to update the inner content to include other information (e.g. like you had an h2 with various styles) you can either put that whole string in the array, or you can use something similar to my duration logic to select the correct wrapper.
Here's the fiddle
edited Nov 22 '18 at 8:32
answered Nov 22 '18 at 6:28
PaulPaul
27k86294
27k86294
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
add a comment |
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
I was hoping to use an array one timer after another alerts are a bad example cuz i think they do pause the screen but when i do the txt id thing the timers fire all at once. Thank you for the fast responds
– pablo
Nov 22 '18 at 6:40
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
If you have other requirements, you should put them in the question. Additionally, it's helpful if you write in whatever code you've tried as it's informative to folks trying to help you as to what you're trying to do.
– Paul
Nov 22 '18 at 6:45
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
@pablo ok, is this closer to what you're trying to do?
– Paul
Nov 22 '18 at 8:33
add a comment |
you can do something like below!
you can modify timeout interval as per your requirement
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understandThe only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?
– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
add a comment |
you can do something like below!
you can modify timeout interval as per your requirement
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understandThe only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?
– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
add a comment |
you can do something like below!
you can modify timeout interval as per your requirement
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
you can do something like below!
you can modify timeout interval as per your requirement
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
setTimeout(myTimeout2,3000);
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
setTimeout(myTimeout3,5000);
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
setTimeout(myTimeout1,2000);
}
myTimeout1();
<div id="tebatademo">
</div>
answered Nov 22 '18 at 7:18
Dhaval PankhaniyaDhaval Pankhaniya
1,6231023
1,6231023
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understandThe only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?
– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
add a comment |
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understandThe only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?
– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
Dude that is almost perfect thx alot. The only issue is the array does not seem to terminate correctly. It always ends with undefined?
– pablo
Nov 26 '18 at 2:22
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understand
The only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?– Dhaval Pankhaniya
Nov 26 '18 at 7:18
@pablo glad it helped and please consider accepting answer whenever it helps you. and i don't understand
The only issue is the array does not seem to terminate correctly. It always ends with undefined?
can you please explain bit more ?– Dhaval Pankhaniya
Nov 26 '18 at 7:18
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
So even in the fiddle the example iterate through the array but towards the end it seems to be infinitely looping without terminating. I've gone through the logic and I can't see where it's going wrong but then again I'm not the greatest JavaScripter. (Getting there)
– pablo
Nov 26 '18 at 14:35
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
Message.shift caused the script to loop forever. Added if (messages.length > 0){ } to fix it..... thx for the heavy lifting
– pablo
Nov 27 '18 at 3:12
add a comment |
Best way to do it with OOP.
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
In case if you need to add other functionality of timer in future, you can easily add and remove it not needed anyone.
Hope this helps!
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
add a comment |
Best way to do it with OOP.
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
In case if you need to add other functionality of timer in future, you can easily add and remove it not needed anyone.
Hope this helps!
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
add a comment |
Best way to do it with OOP.
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
In case if you need to add other functionality of timer in future, you can easily add and remove it not needed anyone.
Hope this helps!
Best way to do it with OOP.
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
In case if you need to add other functionality of timer in future, you can easily add and remove it not needed anyone.
Hope this helps!
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
function Timer(fn, delay) {
this.startTimer = function(fn, delay) {
setTimeout(fn, delay);
}
return this;
}
function timedText() {
let timer1 = new Timer();
let timer2 = new Timer();
let timer3 = new Timer();
timer1.startTimer(myTimeout1, 1000);
timer2.startTimer(myTimeout2, 3000);
timer2.startTimer(myTimeout3, 4000);
}
function myTimeout1() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Mountin Climbers</h2>";
}
function myTimeout2() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: red; color: white; text-align: center;'>REST</h2>";
}
function myTimeout3() {
document.getElementById("tebatademo").innerHTML = "<h2 style='background-color: yellow; color: black; text-align: center;'>Inch Worms</h2>";
}
timedText();
<div id="tebatademo"></div>
answered Nov 22 '18 at 7:31
varit05varit05
2,3501918
2,3501918
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
add a comment |
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
@Paul how it is not? can you please explain?
– varit05
Nov 22 '18 at 8:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424999%2fjavascript-double-timer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown