I recently stumbled across events in Javascript and thought it would be a pretty good method passing Data on certain situations. Now I got the problem, that I dont know how to pass Variables through CustomEvent details correctly. I'd really appreciate if you'd take a look and maybe help me :)
var testVar = 5;
const myEvent = new CustomEvent('testEvent', {
detail: {
myVar: testVar
}
} )
addEventListener('testEvent', (e) =>{
console.log("testVar:" + e.detail.myVar)
})
dispatchEvent(myEvent)
testVar = 2
dispatchEvent(myEvent)
When I use the function dispatchEvent(myEvent)
i thought I'd always get the current Number stored in testVar.
Expected Output:
"testVar : 5"
"testVar : 2"
Actual Output:
"testVar : 5"
"testVar : 5"