Use event listeners to select a row of times#29
Conversation
hourNode.addEventListener("click", addSelectedClass, true);
hourNode.addEventListener("click", hideOrShowEmailButton, true);
hourNode.addEventListener("click", selectOtherCellsInRow(index, selectedIndex, hourNode), true);
This is really obvious if I set the interval timer to two seconds: The email button is added instantly, and the clicked cell is highlighted instantly. But the row is only highlighted after a delay. |
|
Are index and selectedIndex defined at this point? |
|
They should be. I had to pass them as arguments to make it work inside the function. |
|
They may not be at the time the event listener is set up. Add "debugger" (without the quotes, can't do markdown on my phone) above the line the event listener is on and reload the page with your chrome console open. It should pause the script for you and you'll be able to type variable names in the console, if they return as undefined they're not set. Alternately if they aren't set you probably would be getting JavaScript errors in your browser console. |
|
Thanks @samcambridge
function addSelectedClass() {
this.classList.add("selectedhourforsharing");
selectedIndex = index;
}Which is run by this on click: hourNode.addEventListener("click", addSelectedClass, true);Which is above: hourNode.addEventListener("click", selectOtherCellsInRow(index, selectedIndex, hourNode), true);So I thought by the time |
|
Everyone loves Race conditions! |
|
Ugh, right. Thanks. |
|
Do you need an event listener for selectOtherCellsInRow or can you just call it directly from addSelectedClass? |
hourNode.addEventListener("click", selectOtherCellsInRow(index, selectedIndex, hourNode), true);That doesn't run When you write your event listener like that, What I assume you want is this: hourNode.addEventListener("click", function(){ selectOtherCellsInRow(index, selectedIndex, hourNode) }, true);This will run the anonymous function every time the event listener is called, which will in turn run the |
|
To try and explain this a little better, take this example: someFunction = function(){
alert('Called');
};
node.addEventListener( 'click', someFunction );
// This will alert every time the node is clicked
node.addEventListener( 'click', someFunction() );
// This will alert once on file load, and never again even if we click the node |
|
One thing to note is that this may not fix your race condition, but race-condition-or-not with the way you're calling the function currently it's never going to work as expected. |
|
Thank you @levibuzolic that makes a lot more sense. Unfortunately I still haven't got it to work. I tried: function selectOtherCellsInRow(index, selectedIndex, hourNode) {
if (index == selectedIndex) {
debugger
hourNode.classList.add("selectedhourforsharing");
} else {
hourNode.classList.remove("selectedhourforsharing");
}
}
hourNode.addEventListener("click", function(){ selectOtherCellsInRow(index, selectedIndex, hourNode) }, true);If I click on the third row, in the debugger I get: Which is correct. The It's like the function isn't aware of all [].forEach.call(hours, function(hourNode, index) {
...
} |
|
I'll try running the code locally tomorrow and take a proper look. There were a few things I saw earlier that could be tidied up, but I'll have to run it locally to get a better feel for what it's all doing. 👍 |
|
❤️ thank you. |
Conflicts: README.md homeslice.appcache
Conflicts: package.json
Conflicts: assets/app.js homeslice.appcache
|
you still working on this mate? (finally came back to github :P) |
|
👋 HI SAM |
|
Oh hey guys |

Fixes #27
var intervalfor the things that still use intervals to something reasonable. like1000.