Javascript, counting in seconds - adding a point each time - javascript

So I am working on a project of mine and I'd like some help.
I'd like javascript to be able to count in seconds, each second it'd add a preset amount of points.
var total_points = 0
var points_per_click = 1
var points_per_second = 0
function points_per_second() {
docuement.getElementById("current_points").innerHTML("Current Points: " + total_points);
//insert here?
}
I would also like the points_per_second to be able to add into the total_points var. Thanks!

docuement is spelt "document", .innerHTML is not a function call (set it like a variable) & variables and functions share the same namespace, i.e. don't set a variable points_per_second and then declare a function of the same name.
Once you have your syntax errors sorted, you were probably looking for setInterval.
var total_points = 0;
var points_per_click = 1;
var points_per_second = 2;
function update_display(){
var el = document.getElementById("current_points");
el.innerHTML = "Current Points: " + total_points;
//insert here? ... No
}
var ticker = setInterval(function(){
total_points += points_per_second;
// ... or whatever your intended logic
update_display();
}, 1000);
jsFiddle

Related

create objects for loop and pushing them into an array js

I wish to create an array called allEnemies, which contains "bugs" which belong to a Class "Enemy". I'm trying to do it in a for loop because later on the var "bug" will be modified. The problem is that the objects created "if I'm creating them" don't get into the array "allEnmies". Thank you in advance.
var allEnemies = [];
var random_speed = function() {
return Math.floor(Math.random() * (300 - 80)) + 80;
};
var random_y = function() {
var postions = [60, 143, 226];
return positions[Math.floor(Math.random() * 3)];
};
var create_enemies = function() {
var bugs = 4;
for (var i = 0; i < bugs; i++) {
var bug = new Enemy();
bug.speed = random_speed();
bug.y = random_y();
bug.x = 0;
allEnemies.push(bug);
}
};
Demo - http://jsfiddle.net/ubkusb6r/
Ok, so a few things:
1) Since you've declared the for loop using an anonymous function assigned to a variable you need to invoke the function via that variable create_enemies(). Without this, your code won't run.
2) You need to create an Ememy constructor (if you haven't done so yet).
3) You misspell positions at one place in your code...leading to an error.
Take a look at the JSFiddle and let me know if it answers your question (the fiddle includes console.log statements to track the array).

How to get this random

var mess = [];
mess.push('test1');
mess.push('test2');
mess.push('test3');
var num = Math.floor((Math.random() * mess.length));
document.writeln(mess[num]);
This code here generates a random output when refreshing the page. What I want to know how to do is get it to change automatically like dynamically on the page it will change every 5 seconds or so.
How would I go about doing this?
Any help is appreciated Thank you!
Specify a container in your HTML, instead of using document.writeln:
<span id="target">This will be filled by Javascript</span>
Now wrap your code in a function, store its result in the span, and use setTimeout to make it periodical.
var mess = [];
mess.push('test1');
mess.push('test2');
mess.push('test3');
function randomize() {
document.getElementById('target').innerHTML =
mess[Math.floor((Math.random() * mess.length))];
setTimeout(randomize, 5000);
}
randomize(); // initial call to get it going
Put the Javascript in your code after the declaration of the span, or you'll get an error stating that you Cannot set property innerHTML on NULL.
You can use recursive function.
Sorry if I missunderstood your question, but you want something like this right?
window.onload = function(){
var mess = [];
mess.push('test1');
mess.push('test2');
mess.push('test3');
(function r() {
var num = Math.floor((Math.random() * mess.length));
document.body.innerHTML = mess[num];
setTimeout(r, 5000);
})();
};
You can make use of setInterval to generate a new random index at interval of every 5 seconds and then print random value from array.
var mess = [];
mess.push('test1');
mess.push('test2');
mess.push('test3');
setInterval(
function() {
var num = Math.floor((Math.random() * mess.length))
document.write(mess[num]);
}, 5000);
Refer this fiddle
http://jsfiddle.net/3wpcp35L/

Adding variables in javascript

I am looking for some help.
I'd like to be able to add 2 variables and then set one of the variables to a higher digit.
function gain_points() {
var total_points = parseInt(0)
var points_per_click = parseInt(1)
var points_per_second = parseInt(0)}
I'd like to be able to add total_points and points_per_click together, and then for that to increase total_points.
Is this possible?
Having var total_points defined in the gain_points function it will be defined every time the function is called and assigned to the value of 0.
You may want to consider something like this:
var total_points = parseInt(0);
var points_per_click = parseInt(1);
var points_per_second = parseInt(0);
function gain_points() {
total_points = total_points + points_per_click;
}
This allows total_points to continue to increment every time that function is called.
You also don't need to use the parseInt(); The following would be just fine instead:
var total_points = 0;
var points_per_click = 1;
var points_per_second = 0;
total_points = total_points + points_per_click;
Is that what you are saying??
Sorry, can't comment, not enough rep...

Looping two variables and testing every 1000ms

I have two variables:
var frame = $('#avacweb_chat iframe');
var time = $('.date-and-time' , frame.contents()).last().text();
var newTime = setInterval(function() {
var newT = $('.date-and-time' , frame.contents()).last().text();
},500);
As you can see one variable is in a setInterval and the other is not. So technically when I test these in a conditional it has to be inside the interval. Does anyone know how I can get these so that it will update the first variable if the second variable changes and then loop again? EX:
Var Time = [12:30:39 23/05/13]
Var newT = [12:31:39 23/05/13]
So now
Var Time = [12:31:39 23/05/13]
So then we would now need to test the var NewT again. Hope this explained it a little better. I am going to keep trying different things though if you have a suggestion please post it. Thanks guys
I could misunderstood your problem, but use:
var frame = $('#avacweb_chat iframe');
var time = $('.date-and-time' , frame.contents()).last().text();
var newTime = setInterval(function() {
var newT = $('.date-and-time' , frame.contents()).last().text();
if(newT != time)//seems like always true but im just speculating
time = newT;
},500);

Using Javascript to dynamically create dom elements with incrementing ID's

I have a div with an ID "orangeButton" and each time you click on it it creates a new div. This works fine but... I want each newly created div to have an incremental number added to it's ID.
I am not sure how to do this.
Here is a fiddle of the code I have thus far with comments.
http://jsfiddle.net/taoist/yPrab/1/
Thank you
Javascript Code
var applicationArea = document.getElementById("applicationArea");
var orangeButton = document.getElementById("orangeButton");
orangeButton.onclick = function() {
var newDivThingy = document.createElement("div");
newDivThingy.id = 'newDivThingy'; // I want each newly created div to have a numeric value concatenated to it's ID. IE newDivThingy1 newDivThingy2 newDivThingy3
applicationArea.appendChild(newDivThingy);
};​
Am I missing something, why not use a counter?
var counter = 0;
button.onclick = function(){
var newDivThingy = document.createElement("div");
newDivThingy.id = 'newDivThingy' + (++counter);
// continue your stuff here
}
Libraries like underscorejs provide a uniqueid function for this. Otherwise its easy to implement one.
myNamespace.uniqueId = (function () {
var counter = 0; // in closure
return function (prefix) {
counter++;
return (prefix || '') + '-' + counter;
};
}());
Usage.
newDiv.id = myNamespace.uniqueId('newDiv');
Simply use a integer and increment it as each element is added.
var applicationArea = document.getElementById("applicationArea"),
orangeButton = document.getElementById("orangeButton"),
counter = 1;
orangeButton.onclick = function() {
var newDivThingy = document.createElement("div");
newDivThingy.id = "newDivThingy" + counter++;
applicationArea.appendChild(newDivThingy);
}
I have no doubt you have solution and may have forgotten this post.
BUT, I wold like to show a solution that is a compact format.
Note the counter is set to (counter++) so it will start at 1.
var orangeButton = document.getElementById("orangeButton");
var counter = 0;
orangeButton.onclick = function() {
document.getElementById('applicationArea')
.appendChild(document.createElement('div'))
.setAttribute("id", 'newDivThingy' + counter++);
// I want each newly created div to have a
// numeric value concatenated to it's ID.
// IE newDivThingy1 newDivThingy2 newDivThingy3
};​

Categories