Ask your own question, for FREE!
Computer Science 12 Online
mhchen:

Is there a better javascript algorithm for constantly checking a condition? Some websites have never-ending scrolling like twitter, where every time you scroll down, new content comes up. I want to modify the html inside the content that keeps appearing every time you scroll. To do that, I have a recursive function that constantly changes those content every second. For those of you who don't understand what I'm saying, imagine pressing a button to turn something on. The machine checks if the button is pressed, then checks if the button is pressed, then checks if the button is pressed, then oh hey it's pressed, do something, then check if the button is pressed, check if the button is pressed...etc. There's definitely a less naïve way to do this with javascript, but I'm not sure how. I don't think there's event listeners that checks if new content has been loaded after scrolling down.

mhchen:

Here's an example of code I use to add "collapse" buttons on QC posts: ```r function collapseButton(){ let replies = document.getElementsByClassName('replies')[0].children; for(let i=0;i<replies.length;i++){ let button = document.createElement("button"); button.innerText = "Collapse" button.setAttribute('onclick','this.parentNode.parentNode.parentNode.style.display="none"'); button.setAttribute('style','float:right;padding:6px 25px;margin-right:5px;border-radius:10px;'); if(!replies.innerHTML.contains("this.parentNode.parentNode")){ replies.children[1].children[4].appendChild(button); } } setTimeout(collapseButton,2000); } ``` As you can see, every 2 seconds it checks if a button has been added to a post. If not, then add it. It goes on forever

CoolRekterizer:

Nice, I like this stuff, but put that on Miscellaneous, this is not science...

Gdeinward:

It could count as science.

CoolRekterizer:

True, true.

Ultrilliam:

In terms of QC you could actually add a hook to the vent of a reply being posted or a question being posted and then wait for the 2 ticks to then apply the button. That would be the most efficient way

Ultrilliam:

Code for Replies ```javascript document.addEventListener('insert-reply', function() { //code here }); ```

Ultrilliam:

And you could also wait for the event when someone views a question and insert the buttons instantly that way, that's how the Quote button used to work`

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!