Looking for a simpler JavaScript timer - javascript

Right now i have a text-based game that deals with turns, but i want to upgrade it to a timer system so that when the timer hits 0 then you gain 1 or 2 of whatever action you were doing. Also if you know how to make it randomly pick whether u get 1 item or 2 items could you please elaborate on how to do that? Thank you to all responses.

You can use setTimeout() for delaying actions for a set period of time, or setInterval() if you wish to run it periodically on a given interval.
For getting a random number either 1 or 2 you can use random():
var randomOneOrTwo = Math.floor(Math.random() * 2) + 1;

Hope you will get from here.
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
Happy Coding.....

Related

Javascript per second calculation

I am currently wracking my brain to work out what should probably be a relatively simple per second calculation. I have a loading bar increase and at the end of that, it adds 1 to the total. The loading bar consists of:
wId = setInterval(worker_identify_call, wIdSpeed);
and
function worker_identify_call(){
worker_identify_amount++;
wElem.style.width = worker_identify_amount + '%';
}
wIdSpeed = 250.
I am trying to calculate how long, in seconds, it will take to reach the top of the loading bar (100%).
I currently have ((1000/wIdSpeed).toFixed(2)) but that just calculates how long a cycle of setInterval takes.
CodePen with example here.
Any help is appreciated!
If you want to recalculate after every cycle you have to move workerString(); to inside the function that loops.
As for the math, you need to get the remaining (100 - worker_identify_amount) and check how many things it's adding per second and figure out the result from that.
I'm in idiot. For anyone that wants to find this in the future, it would be:
(((wIdSpeed/1000)*100).toFixed(2));
Where wIdSpeed is the speed of your interval.

Make a beat machine in js

I wanna make a beat machine to play sounds at a given time. I need to pass in a bpm number. My first attempt was just to use a setInterval function to get a constant beat, but Im not sure if thats the way to go.
let beat = (bpm) => {
setInterval(()=> {
// Run update beat machine
}, (60 * 1000) / bpm)
}
I didnt find anything on the web. I appreciate articles as well.
thanks
No, that's not what you want. setInterval is not precise.
The Web Audio API has an AudioBufferSourceNode which keeps the sample in memory. It can be scheduled to start at a certain time.
bufferNode.start(0.5, startOffset);
In this example, we start playing 0.5 seconds from the offset.

How to make a very simple countdown timer in Javascript?

I'm making a simple game which generates random numbers and user has to enter a value, if users value matches the random value generated, the user wins basically this is a beginner project. I want to make a small counter so that user has a limited time to put in the value and I want that limited time to show on the screen in a label. Lets say that you have to put the value under 30 secs and the timer will count from 1 to 30 every second. The counting from 1 to 30 will update in the label every second. This is the logic I'm trying to work on right now and I can't figure out any other way... If I've done some mistake in code please comment or if you have much more simpler way please post it down below. (pls dont vote down im at threat of account suspension)
Heres the part of my timer code:
if(timer <= 30)
{
for(var i = 0;i >= 30;i++)
{
setInterval(null,1000);
timer++;
document.getElementById("counter").innerHTML = timer+" seconds wasted";
}
alert("Time is over, you lost by "+s);
}
You could create a recursive function.
Say var countDown function(){ time = time--;
setTimeout(countDown, 1000);}
Then you need a variable time that is accessible for the countDown function.
var time = 30;
In the countDown function you could create an updateTimeElement.
Try it out.
The setInterval function has 2 parameters, a callback (an anomynous function in javascript thats triggered) and the milliseconds between each trigger of the interval.
What you are doing in your script is making an interval with nothing to do each second (this runs indefinately), then increment the timer integer and updating the DOM. However, this all executes within seconds.
I'd suggest (before you use a function) you look at the documentation to see how you can improve your script to work as you intent to ;-) Here are a few links that might help you get started:
http://www.w3schools.com/js/js_timing.asp
https://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/
I wont be doing the work for you, since this is a good exercise for a beginner programmer ;-)
If you can't figure it out, leave a comment below this answer and I'll get back to you to help you if you need further assistance.

Sync displays using current time

I'm making a digital signage system and I'd like to sync the displays so that they all show the same screen at the same time. I'm thinking that the simplest way of doing this is using the current time as a marker - all the machines running the displays will have the accurate time, and the amount of slides they have will be the same.
Is there a calculation I could perform on the current time to work out which slide to display which could be used on each screen, with a slide display time of 30 seconds for example?
I have no experience working with this.
But speaking with a colleague whom has, he pointed me in the direction of Synchroscope.
Seems like the right way to approach it?
Hope it helps!
Synchroscope Guide
Thanks to Bergi's suggestion of using Modulo, I've found a solution. Here is my Javascript code that changes the screen/slide every 30 seconds:
var sec = Math.floor(Date.now() / 1000);
var nearest30Sec = Math.round(sec / 30);
var currentSlide = ((nearest30Sec - 1) % 10) + 1;
currentSlide will produce a number between 1 and 10, which will change every 30 seconds.

setInterval function using jquery is causing a "blink"

Personal project using jQuery.
I'm trying to create a function that runs on the hour for 5 seconds. I've done this by getting the current minutes and acting when they are at '00'. (Although for testing the minutes need to be manually changed to the next minute, unless you want to wait an hour to see it run again.)
The function acts on 2 objects, one to add/remove a class, the other to slideUp/Down.
It works, but after the initial running, the slideDown/Up jQuery causes a "blink" every 5 seconds for the rest of the current minute.
I've tried setting the setInterval for 5000, however that hasn't solved the issue. I'm at my wits end really.
While I am also using moment.js elsewhere. This function isn't using moment(). Primarily because I haven't been able to get functions working with moment() either.
Just head to the ....
jsFiddle example
Remember to set the =='00' to the next minute -- sure makes testing easier I really appreciate anyone waiting for this to run. I know it can be a pain to have to wait a minute to see the function at work.
If you watch the function run for 5 seconds, it will stop... but continue watching.. the slideDown() will repeat every 5 seconds until the minute is no longer XX.
How can I stop this repeat??
Thanks!
There're two place for fix.
1. miss usage for 'clearInterval'
clearInterval parameter is The ID of the timer returned by the setInterval() method.
reference this link, w3c definition for clearInterval.
var intervalId = setInterval(function() { alarm(); }, 5000);
...
clearInterval(intervalId );
2. secs >= "05" condition is wrong
change string "05" to int 5.
Believe it or not I sorted it a few moments after posting this.
My conditional was off, and I thought I tried everything. Guess not.
This works
if((mins == "29") && (secs <= '05')) {
$('#focus').slideDown(500);
$('.projcnt').addClass('jump');
} else {
$('#focus').slideUp(300);
$('.projcnt').removeClass('jump');
}
And the ...
working, updated fiddle

Categories