Makes sense, since that's what you're telling it to do. Any and all data, go to "r1".
Yup, also makes sense. It's the equivalent of the pizza in which everything get's called, since there's nothing separating them.
That's most likely because you're using the then structure incorrectly. It seems as though it takes only one function, and you're trying to have it handle four.
Not sure why you dragged that requestURL into this. It serves no function here. Besides that, it isn't declared properly as a variable (though JS is quite forgiving on that). However, the then function calls a function, and passes it a variable as an argument, which you've now called requestURL. It doesn't care what you've made that to be earlier. Hence, the last two codes are functionally identical.
You need to find the balance between the first and second example. You only want it to show in a certain place (1st example), yet call that from within the same then part (2th example). Earlier, you mentioned:
I was having a having a hard time getting the if-else statements to work, so I gave @ScruffR recommendation a try. Took me a bit to work out all the details, but in the end the code below is working perfectly.
Splendid! That was actually going to be the next proposed step.
For completeness sake, this is the if variant:
function variableGet(variableName){
var vrGr = particle.getVariable({ deviceId: ActualDeviceID, name: variableName, auth: token });
vrGr.then(
function(data) {
if (variableName == "getRelay1"){
document.getElementById("r1").innerHTML = data.body.result;
}
else if (variableName == "getRelay2"){
document.getElementById("r2").innerHTML = data.body.result;
}
else if (variableName == "getRelay3"){
document.getElementById("r3").innerHTML = data.body.result;
}
else if (variableName == "getRelay4"){
document.getElementById("r4").innerHTML = data.body.result;
}
}
);
}
Now, the one youāve currently got can be made even easier, without the need for the additional r argument. Youāre already passing through a unique identifier in the form of the getRelayX. Itās a bit double to then add rX, isnāt it? How could you make that simpler, using what youāve already got? hint: āspan idā
ahhā¦I see what I was doing wrong hereā¦I was trying to do the if-else statements without hard coding the getRelayX identifier. But now it makes senseā¦
As for the second partā¦I just want to clarify a few things. With thisā¦ It's a bit double to then add rX are you referring to the rX parameter added to the html buttons? Are you saying that can be removed from the button parameters?
Yup. You can't 'just' strip it, you'll have to make another small adjustment.
Try to figure out what the things below have in common as far as identifiers go. How could you put that information in one argument (since getRelay1 and r1 are basically indicating the same thing...)?
Both getRelay1 and r1 are basically saying that you need the first relay/span. Now, the getRelay1 is the variable name, so you canāt change that easily. There is however something that uses r1 that can be changed easily to getRelay1, thatāll allow you to use that instead of r1, thus removing the need for r1 entirely.
In my last post, try taking r1 out entirely (everywhere), and figure out what youād have to insert/change to still make it work.
you got me on this oneā¦I know r1 is needed in the document.getElementById(""), without it no info is passed to that string. We still need a parameter or string passed to document.getElementById("") to print the data on the html page.
I tried thisā¦but in order to eliminate the need for multiple strings of javascript, donāt weed need to declare whats in the parenthesis as a variable? Or am I completely off track hereā¦
Wowā¦this is really rattling my brain. The span IDās cannot be the same from my understanding.
This doesnāt work, nor does putting parenthesis around the 1 of getRelay(1) so some how you have to tell the document string that relay number is variable no?
I said you had a variable there, I didn't say which one, nor did I say it had to be that exact one.
Seeing as you're no longer inputting r into the function, you can't use it. You've now only got one 'argument' at you disposal. Might want to use that one
What element is this going to be looking for as it stands now, and what should it be looking for?
Gotcha! Iām not at home anymore so Iāll look later. I guess I shouldnāt take you so literally. Thanks for the help. Iām getting better at understanding things once I see them.
So let me know if this is correct, just in terms of parameters, arguments and variables. onclick="variableGet('getRelay1')" in this case, getRelay1 is the parameter correct?
function variableGet(variableName) in this case its calling the variable, which is getRelay1, or getRelay2, or getRelay3ā¦correct?
And it seems the argument as well is getRelay1 and getRelay2, or is the argument just getRelay? Or am I completely wrong.
I did try this code, and I donāt get any errors in the console, but I also donāt get any results.
Itās better to see it happening than me explaining it.
To see what comes in through function variableGet(variableName) you can add console.log(variableName) underneath it, which should print the variable to the console. Try some different values in the call to that function, and you should see what goes in/comes out.
Within the āthenā part you can also add a console.log to see whatās going on.