+ 1

JS how can I {do something} when an element or variable is is defined.[SOLVED]

Do something when an element becomes defined. In this code, x is not defined to start with, but after a second, it will be created and defined. I want to do something when it becomes defined or exists. https://code.sololearn.com/WoO62sIY0YCN/?ref=app

26th Apr 2020, 8:33 PM
Ginfio
Ginfio - avatar
13 odpowiedzi
+ 6
Ginfio I've got a solution for you in this code. https://code.sololearn.com/WDMIm09AYBWB/ However, be sure to read the message I left you in the HTML comments. I also left you a list of questions to challenge you a bit further. No worries if you don't respond. 😉
27th Apr 2020, 10:28 PM
David Carroll
David Carroll - avatar
+ 4
Ginfio you should not redefine x in setTimeout, it would make x as local variable in the timeout function only, which is different from x defined in global scope. To keep checking the x variable, you could implement setInterval function, rather than just calling x directly.
27th Apr 2020, 2:09 AM
Calviղ
Calviղ - avatar
+ 4
Ginfio you cannot access local variable of other function, if you really can to protect the x variable from being accessed globally, you could make a function to include all the setTimeout, setInterval callback, including x variable there. https://code.sololearn.com/W5l6S2d9FAtw/?ref=app
28th Apr 2020, 3:09 AM
Calviղ
Calviղ - avatar
+ 3
Calviղ I meant to only define x in the timeout_ So, how would I check when x is defined using setInterval() setInterval(function(){ check when x exists / is defined }, 100) And when it does exist, or if it exists (defined) {do somethin} set
27th Apr 2020, 2:18 AM
Ginfio
Ginfio - avatar
+ 2
Ginfio To check for existence of defined variable, you should use typeof Eg. if(typeof x !== "undefined") it would return false if x is not exist, rather than runtime error. The typeof operator returns a string indicating the type of the unevaluated operand. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
27th Apr 2020, 9:21 AM
Calviղ
Calviղ - avatar
+ 2
Calviղ I coud use that if(typeof x !== “undefined”) But that’s gonna return false, because x is not defined to start with. I’m wondering if there is a way to check every certain time. like.. setInterval(funcion(){ check if x exisits. },100) every 100ms it would check for x’s existance until it does exist. And wen it does exist_ do somethn
27th Apr 2020, 4:09 PM
Ginfio
Ginfio - avatar
+ 2
Ginfio it's variable scoping issue in your code. 'x' variable which is defined inside setTimeout() function is in local scope. It can't be accessible to outer scope. And that's why error showing - 'x' is not defined. Solution to working code - 1) define 'x' variable global (outof the function). 2) put your if block inside setTimeout function. reference: https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70
27th Apr 2020, 7:39 PM
! Bad Coder
! Bad Coder - avatar
+ 1
I found this on stack overflow, but it won’t work on my code_ function waitForElement(){ if(typeof someVariable !== "undefined"){ //variable exists, do what you want console.log("738") } else{ setTimeout(waitForElement, 250); } }//
27th Apr 2020, 4:55 PM
Ginfio
Ginfio - avatar
0
Well you are already doing something when it is define ,so I Don't understand do you want to do something when it is not defined ?
26th Apr 2020, 8:57 PM
Abhay
Abhay - avatar
0
Yup you are already printing something ,when it is defined but again it becomes undefined as setTimeOut function finishes
26th Apr 2020, 9:52 PM
Abhay
Abhay - avatar
0
Abhay hmm_ Let’s look at it like this: I removed the x.innerHTML = “...” from the setTimeout() now x.innerHTML is unset / nothing. there is a delay (timeout, 1000) before x will be defined. Until that 1 second (1000ms), x will be undefined. Once that 1 second is passed, it WILL be defined. Now, can I run a test to check if x is defined. Like maybe, every 100 seconds check if x is defined. 100ms: x = undefined (no exist) 200ms: x = undefined (no exist) .... 900ms: x is still undefined 1000ms, now it’s defined (does exit) When it is defined, exists, do something_
27th Apr 2020, 12:42 AM
Ginfio
Ginfio - avatar
0
David Carroll I looked at the questions, and I was gonna write down the answers, but I thought z answers might just be long and bzazzz So, i thought I might just tell you the ones that I don’t understand: 4 && 5 4: “Why would this scenario for conditional behavior based on the existence of a variable not be possible in a strongly typed and/or compiled environment?” umm, maybe because the variable might get mixed with other variables, or umm_.. 5: I’ m not sure why this would be a bad design to follow, I mean... IF this is a bad design to follow, does that mean there’s a better design, Or is it lole _ just don’t follow this design?
28th Apr 2020, 6:12 AM
Ginfio
Ginfio - avatar
- 3
Thats good
27th Apr 2020, 10:39 PM
Austin Chinoda
Austin Chinoda - avatar