updating global variables javascript/html [duplicate] - javascript

This question already has answers here:
Persist variables between page loads
(4 answers)
Closed 7 years ago.
so currently I am building a website where I get data from google spreadsheets and then graph it on my website...
However, because this data is constantly streamed from somewhere else, I need to be able to show 10 data points on the graph every time. (So shows 10 data points then the next 10 and then the next 10 and so on). So I personally need 2 global variables to keep track of where in the data I am thus be able to present the user with the correct graph.
I have tried this...
<script>
var i = 0;
var lengtharray = 0; //This variable is the second global variable that I need and is updated within the same function as i.
function doSomething(){
anotherFunction();
i = i + 10;
lengtharray++;
}
</script>
so the next time the script is run to show the next data point on the graph, the i value that I expect is the previous i value + 10 and the lengtharray value to be incremented...
instead, what I get is i = 0 again and lengtharray = 0 again..
I need these values to be "updated" and only initialised/reset when I want it to be/the browser closed and reopened on the webpage...
I hope this question all makes sense!
Thanks in advance.

If the problem is due to those globals and the function being re-instantiated every time then you might just be able to use a singleton pattern to prevent this. If you are unfamiliar, it is a design pattern that always returns the previous instance of your class, variable, whatever. That way there is always only one instance of your object, class, variables etc. I have linked the implementation in javascript here

Related

How can I recieve messages from Web Workers when they have the same name?

I am currently learning Web Workers in JavaScript, and therefore, making a script says something 8 times, every thread does 1.
So to make it, I use this code:
main.js
for (let workersAmount = 0; workersAmount < 4; workersAmount++)
{
var socketWorker = new Worker("worker.js");
}
socketWorker.onmessage = function(event)
{
console.log(event.data);
}
worker.js:
postMessage("Why can't it just work!")
And this is the result (this is my first post here so I can't embed):
image
As you can see, it only shows the text 1 time, while there are 4 workers. Which (I think) is because is replace the variable "socketWorker" everytime, but still keeps the old worker.js
So, does anyone know how to fix this? Thanks in advance for you help!
They don't have the same name. They don't have names at all.
The socketWorker variable has a name. It also has a value.
First it has the value that is a reference to the first worker. Then you immediately overwrite it with the value that is a reference the second worker. And so on.
When you overwrite the first reference you no longer have that reference.
You have thrown it away.
You can't get it back.
By the time you get to the line socketWorker.onmessage = function(event), you've only got the last reference you stored in socketWorker available to you.
Assign a value to the onmessage property inside the for loop, before you overwrite the value of socketWorker.
If you need to keep a reference to each of your workers around for the future, then push them into an array instead of using a single variable which you keep overwriting.

How can I add permanent data into an variable

I'm working on a project that plays a game of rock paper scissors
the game works fine but i want to add something into it. a pointing system where it will add 1 each time a player win but i can't seem to make this work i've been trying to find workarounds for almost 2 day now but i can't seem to find any solution. The thing is the code works but after i log the function that adds 1 to a variable but after that it returns to it's original value which is 0.
const one = 1
function addone(test){
test += 1
}
addone(one)
console.log(one)
This print the original value like the function did not do anything to it. im so confused.
I see two problems in your code:
one is a const variable. That means that you can't modify its initial value.
test is a local variable inside your function, that means function won't modify the values of the variable that you pass as parameter (one).
There is an operator(++) that add a unit, I highly recommend you to use it instead of make a function:
var one = 1
one++;
console.log(one)
If you want to use a function you have to do like this:
var one = 1
function addone(test){
return test+1;
}
one = addone(one);
console.log(one)
In terms to store permanent data into a javascript variable, that's not possible. Variable are stored in RAM, that means that when you stop the program, al data of your program are wiped. Permanent store are made in database and other complex data structures.
If you want to play multiple games, you should consider to allow the players to play more games before the program finish.
thank you very much i never think that i could get more dumb so i cant manipulate it because it's just a copy of that variable that im inputing as parameter? so it means that the function did not touch the variable at all. I wish i could go back to my mother's womb.
and the const it's just a typo very sorry for this.
i did not know this website is very active i thought i would wait days just to get a response thank you very much and have a nice day

set javascript intial variable to data scraped from website

I am trying to set a variable in javascript to a value retrieved from another source. For instance, in this case, I have a calculator that is calculating estimated payments. Currently initial variables are set as follows....
// Initial Values<br>
var data; // To be used by chart<br>
var homePrice = 250000;<br>
var downPaymentPercent = 3.5;<br>
var interestRate = 5.25;<br>
var user_state = "FL";<br>
Two questions I have are...
1.) What would be the easiest way to set the interestRate variable equal to todays 'current rates' as shown by an external website such as...
www.wellsfargo.com/mortgage/rates/purchase-assumptions?prod=1
Also open to learning of a better way if there is a more straight forward method than this
2.) user_state is currently set through this initialisation list, what method would be used to set this state based on a users IP address?
My main interest is in solving question 1 right now, I am curious to question 2, but the first question is more important at the moment.
Thanks!

JavaScript: Accessing Variables from Other Files and Changing their Properties

So I am making a hacking type game coded with HTML and JavaScript, and I made a Store.html file for my store to purchase upgrades for your hacking system. I have several variables that I want to view and change from a different file than the one that I originally have it inside of.
Variable 1
var tutorialDone = false
Variable 2
var jobBoolean
Variable 3
var bitCoins = 30
var currentBit = bitCoins
I want this to be false because when the tutorial mission is finished, then I want to change the jobBoolean variable to "true" because when the tutorial mission is completed, then I want a new list of missions/jobs to show up on thee screen. The first mission is a seperate page. At the end of the mission when it is completed, then "tutorialDone" I want to be equal to "true", so that when I am back at the homepage, I can change the jobBoolean to true. Because the "tutorialDone" variable is dependent on the "jobBoolean" variable. When "tutorialDone" equals "true" so does "jobBoolean". I don't know how to make "tutorialDone" change along with the "jobBoolean".
I have for the third variable, there is two. That is because they are similar. I have a function inside the "Store.html" document that when an item is purchased, it grabs a variable which in this case is "currentBit". "currentBit" is equal to "bitCoins" because I thought that if I made a new variable, "currentBit", then set it equal to "bitCoins", it would connect with "bitCoins" from a seperate file. This was my only "solution" I could think of, but it doesn't work.
Also, as a bonus answer for you guys, how can I make an "if" statement in JavaScript, and then have it check if the variable I give it is equal to something, and if it is, run some code. Because whenever I try it, the function the if statement is in, it will run through the if part of the statement no matter if the statement is true or false.
Sorry if this question doesn't make much sense. This is just the first big problem I have encountered with JavaScript, that I couldn't fix myself.

How to set the number of copies to print from javascript [duplicate]

This question already has an answer here:
javascript window.print(), passing number of copies as param
(1 answer)
Closed 7 years ago.
I am trying to print a document using print method of javascript.
Now i want to set number of copies to print(by default it will print 1 copy).i want to set the number of copies to two(2 copies).
Is it possible to do it from js code.We can set by printer settings,but it will print for all the copies.I need it for specific document to print 2 copies.
My code is :
$scope.printPDF = function (id) {
window.frames["doc1"].focus();
window.frames["doc1"].print();
window.frames["doc2"].focus();
window.frames["doc2"].print();
window.frames["doc3"].focus();
window.frames["doc3"].print();
};
Now i want to print doc2("only doc2") as 2 copies.remaining should be default 1 copy.
Thanks in advance.
There are no additional parameters for the window.print() method and definitely no parameter that allows you to specify the default number of copies.
The only workaround for this that I can think of would be to call the print() method twice on doc2. Obviously not the greatest user experience.
It is not possible, one solution would be to call the print twice
window.frames["doc1"].focus();
window.frames["doc1"].print();
window.frames["doc1"].focus();
window.frames["doc1"].print();

Categories

Resources