+ 1

How do YOU(specifically, you) watch for variable changes in your script? #javscript #AllOtherCodeToo

<!-- This is like a challenge, and a question, and just me trying to talk out a problem I'm having. Well, currently it's not a PROBLEM. But I don't think it is technically "Best Practices" and mightcould qualify as some light hacking... lol Using a similar system to what I write here to detect HP and MP in my HTML5 game, and keep the bars filled at the proper level. Let's say: My style classes are as follows. --> <style> b { display: none } i { display: inline } </style> <!-- Here's some HTML to interact with it.--> <body> <header>Header</header> <!--/* Becomes the footer */--> <i>Information</i><b>Paragraph</b> <button onClick="switchItUp()">Switch it up</button> <!-- My script says --> <script> var x = "inline" var y = "none" function switchItUp() { document.getElementsByTagName("b")[0].style.display = x; document.getElementsByTagName("i")[0].style.display = y; y = document.getElementsByTagName("b")[0].style.display; x = document.getElementsByTagName("i")[0].style.display; } function getHeaderFooterSwitch() { if (document.getElementsByTagName("i")[0].style.display === "none") { document.getElementsByTagName("header")[0].style = "position:absolute; bottom: 0px; padding-left:80%;" } else { document.getElementsByTagName("header")[0].style = "position:absolute; top: 0px; padding-left:80%;" } } setInterval(getHeaderFooterSwitch, 30) //<----------- Here's My Issue </script> <!-- So, it works. But the issue is, I know that calling a function every couple milliseconds isn't exactly the best method. This is just a simple example, there are plenty of more complicated ways a code like this could be used. //Commented for simple CnP to Code Playground. ------------------------BOTTOM LINE----------------- What's a better/the BEST way to watch for variable changes? And how do YOU do this? -->

3rd Jul 2017, 10:21 PM
Russel Reeder
Russel Reeder - avatar
5 Antworten
+ 3
Cool. Read respond to bottom line? Or question that was asked? That would be cool. Here's the code if u just wanna run it. https://code.sololearn.com/Wym9D6UC951F/?ref=app
4th Jul 2017, 1:10 PM
Russel Reeder
Russel Reeder - avatar
+ 3
Thanks for the feedback. I'll check out the code when I have more time.
10th Jul 2017, 1:43 AM
Russel Reeder
Russel Reeder - avatar
+ 2
tldr, too much spaghetti, cant even bother to read. dont expect ppl to read the whole chunk
4th Jul 2017, 1:08 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 1
getter setter fiddle I looked at for reference https://jsfiddle.net/5o1wf1bn/1/. good luck
10th Jul 2017, 2:22 AM
James Mclean
James Mclean - avatar
0
I was looking at this as well. First I will tell you what I did. I ended up throwing an event with parameters and catching it. See paint Surface Events. Second you can use getters and setter (haven't tried this yet but looks like you can wrap function calls inside them). Note I'm learning this as well so this may be wrong. https://code.sololearn.com/WproocS67By9/?ref=app
10th Jul 2017, 1:40 AM
James Mclean
James Mclean - avatar