This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Text blinking jQuery
I'm trying to create a timer in my HTML5/JavaScript game when the game is over. Something like 3...2...1.. where 3 will appear then flash, then 2, then flash, then 1, then flash and return back to my title screen... Nothing I put will get that to work... Anybody able to help me out here? So far my code for the numbers is this:
function CreateTimer(){
timer = setInterval(function() {
cntxt.fillText(time, c.width/2 - cntxt.measureText(time).width/2, c.height/4);
time--;
}, 1000);
}
function resetTimer(){
clearInterval(timer);
time = 3;
where = "menu";
}
But even this will just place the 2nd number and then 3rd number straight over the previous and then return the title as expected.
Thanks in advanced!
You need to clear the text first see http://www.w3schools.com/html5/canvas_clearrect.asp
Related
This question already has answers here:
JavaScript: get code to run every minute
(3 answers)
Closed 5 years ago.
I'm new to html and javascript but I was wondering how I could go about sending alerts like this https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_alert every minute or so.
setInterval(() => {
// your code
}, 1000)
https://www.w3schools.com/jsref/met_win_setinterval.asp
javascript have a method to repeat something like a task every so often, this method is the function setInterval this function repeat that you want.
Javascript:
setInterval(function(){
alert("Hello");
}, 3000);
For example this method shown every 3 seconds an alert that say "Hello", if you want repeat a task every 3 minutures you need change 3000 to 180000 like this:
setInterval(function(){
alert("Hello");
}, 180000); // repeat every 3 minutes
Here is the docs of the function.
I hope it helps you, regards!
So far I have a little script that detects the scroll top position and at a set level I want it to trigger a jquery counter. So far I have an array with the maximum number inside var = eightyS = [3]; then there is..
if (y > 630) {
$('.targetS').each(function() {
//counter
delay(1000);
});
} else {
return false;
}
Now I've made something similar in C++ years ago (couldn't do it now with a gun to my head) so I followed a similar logic. But this is where I'm stuck. The idea behind this function is that it will do a read out on screen of 0 then 1 then 2 then 3. Any help is greatly appreciated
You could use a setInterval() which executes a function ever second such as below:
var count = 0;
var interval = setInterval(function(){
count++;
$('#counter').text(count);
}, 1000);
I've created a quick JSFiddle
You should be able to wrap this in to your code fairly easily. You may also want to use clearInterval(interval) to stop the function executing when you scroll back up the page; or when you get in to your else block, which would have the same effect. I've added a clearInterval() example to the JSFiddle on click of the stop link. You'll need to make sure the interval variable is in scope when clearing it.
This question already has answers here:
Open links, one after another
(3 answers)
Closed 8 years ago.
I was trying to make a program that opens a link, closes it after 3 seconds and opens the next one. I'll add more links, but I'd like the program to ask every 10 links "Are you there?". I thought of doing maybe an if statement, but not sure.
<script>
var links = ['www.youtube.com', 'www.yahoo.com', 'www.google.bg', 'www.facebook']
function openLinks(){
window.open(links[0]);
links++
}
function withIntervals(){
setInterval(openLinks(), 3000);
}
</script>
Something like:
var currIndex = 0;
function openLinks(){
window.open(links[currIndex++]);
if (currIndex % 10 == 0) {
if (confirm("Are you there yet?")) {
// do something
}
}
}
When currIndex is divisible by 10, the remainder will be 0, so your if condition will be true.
You'll probably also want to keep the value returned by setInterval so you can clear it later with clearInterval.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a div's text to turn red when you start counting, it should lasts 3 seconds and leave the red text while counting. Then return back to normal when it finishes (and regular displays when stopped 3 seconds)
Displaying the count in milliseconds.
I tried, but it still fails.
Something like this?
var foo = document.getElementById('foo');
function startCountDown(ele) {
ele.origText = ele.innerHTML;
ele.style.color = "red";
ele.timeStart = (new Date()).getTime() + 3000;
ele.intervalVar = setInterval(function() {
var curTime = ele.timeStart - (new Date()).getTime();
if(curTime < 0) {
ele.innerHTML = ele.origText;
ele.style.color = "";
clearInterval(ele.intervalVar);
}
else ele.innerHTML = curTime;
},20);
}
setTimeout(function() {startCountDown(foo);},1000);
<div id = 'foo'>bar</div>
Innovative/Alternative way to show timers using requestAnimationFrame,
http://jsfiddle.net/wjtkr95t/4/
wich has less impact to the browser's memory.It also allows you with some minor modifications to have more control over timers,animations ... whatever...
var
end,
now=Date.now,
raf=window.requestAnimationFrame,
duration=120000,//MS
out=document.getElementById('out');
function displayTime(){
var c=end-now();
out.textContent=ms2TimeString(c>0?(raf(displayTime),c):0);
}
function go(){
end=now()+duration;
raf(displayTime);
}
Ms to timeString function
function ms2TimeString(a,k,s,m,h){
return k=a%1e3,
s=a/1e3%60|0,
m=a/6e4%60|0,
h=a/36e5%24|0,
(h?(h<10?'0'+h:h)+':':'')+
(m<10?0:'')+m+':'+
(s<10?0:'')+s+'.'+
(k<100?k<10?'00':0:'')+k
}
https://codereview.stackexchange.com/q/45335/33435
DEMO
http://jsfiddle.net/wjtkr95t/
DEMO (change the color) .. only 100ms.. change it
http://jsfiddle.net/wjtkr95t/1/
DEMO (change the color after 2 sec) .. only 100ms.. change it
http://jsfiddle.net/wjtkr95t/2/
DEMO (change the color after 2 sec with 700ms color animation)
http://jsfiddle.net/wjtkr95t/3/
if you have any questions just ask
I think if you show us your algorithm it would be clearer for anyone who want to help you. Waiting it, I would recommend you to see how the function setInterval() works, because it puts a delay so you probably would use it to reach your goal.
Go here to see docs.
This could be done using CSS animations, and adding a class to a div element.
This method would be better, since CSS animations are not blocking, whereas since javascript is single synchronous thread (for the most part); and so using setInterval will block the rest of the javascript from functioning.
This question already has answers here:
How to randomize (shuffle) a JavaScript array?
(69 answers)
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 9 years ago.
I have a repeating page loading function here,
<script>
var interval = 5; // in seconds
var pages = [
'http://example.com/index.php',
'http://example.com/about.php',
'http://example.com/services.php',
'http://example.com/portfolio.php',
'http://example.com/career.php',
'http://example.com/contacts.php'
];
var current_page_index = 0;
setInterval(function() {
loadintoIframe('myframe', pages[current_page_index]);
current_page_index = (current_page_index + 1) % pages.length;
}, interval * 1000); // second setInterval param is milliseconds
</script>
Working fine but I would like to change its loading pattern to RANDOM. Now it is loading as it is given in a pattern.I mean it will first load 'http://example.com/index.php' then 'http://example.com/about.php' like that.
How can I add random effect to it? Someone help me pls....
This question is the extension of Load different pages with in a single iframe
Rather than iterating through your page indices, just get
pages[Math.floor(Math.random()*pages.length)]
If you want to avoid duplication, ie. go through the pages in a random order, then keep your current code but - before the setInterval - shuffle the array. Personally I'd use
pages.sort(function(a,b) {return Math.random()-0.5;}); But I know there are picky people out there who will say this isn't "random enough"... -shrugs-