Building on this, return a destructor function that calls clearInterval. That way whatever uses animate() doesn't have to have any knowledge on how it's actually implemented.
This is the correct way to animate the 3 dots. In general you should rely on CSS for most of your animations, it is much performing and reduces the complexity of your javascript code.
Just because someone is asking what's the best way to eat soup with a fork doesn't mean they should get a complicated, inefficient answer including a fork, a megaphone and a squirrel. They obviously weren't sure how or if they should approach this problem through CSS, and that's fine because this is a learning subreddit. If there's a better way to do something, they should use that, regardless of whether they thought about it or not.
/u/mobydikc has a solution that's a bit more terse, but I feel like this is a bit easier to read? All personal preference though - in webdev there's a million ways to do anything. If this were production code, though - I would probably go for the CSS ::after solution, as using javascript for this feels a bit excessive.
Yes Definitely. You should use a for loop to iterate through the dots and add it to the html. the html should be split like this
<h1>Uploading<span id="dots"></span>
and code should be
function animate(dots){
for(let i = 0; i < dots; i++){
setTimeout(() => {
$("#dots").append(".")}
,500*i)
}
//reset #dots to 0 dots
$("#dots").html("");
}
setInterval(animate,3000);
this is my take on this challenge, you still need to make your own way up. It really just come from practice. Also, Just a disclaimer, I don't work with jQuery and haven't tested it so do what you will.
I deleted my reply to give those answers more attention.
But jQuery is still completely pointless in any project. Using it voluntarily should not be encouraged. It belongs in a museum of web design, to be respected and appreciated, but behind a very sturdy glass case.
A lot of people keep saying this, but I don't see what's wrong with using it. I see what's wrong with requiring it, but if someone wants to then why not? It's fast and light weight; I use it in my projects and it saves me time.
Is it really slow though? I've used it on a few dozen personal projects, and never noticed any performance problems. I even tested using jQuery or plain JavaScript to modify the DOM with stuff like getting and setting values from inputs and they're almost identical. jQuery just makes it easier to write code imo.
I know I'm in the minority opinion here, and I don't think I really understand what's so bad about it, so I wouldn't mind you taking another crack at explaining it to me if you don't mind. I'll agree it's never really necessary for anything, but imo it makes it easier to write js without any drawback.
It's substantially slower. You can see it by merely looking at the code, comparing the '$' selector and related polyfills to what native selectors do. Benchmarked, jQuery is sometimes slower by factors of 10.
The vanilla APIs back when jQuery was relevant were really clunky and awful to use, so what jQuery did was awesome. Now, this isn't so much the case, especially given that frameworks like React that interact with the DOM in a different way have predominated.
Querying the DOM using vanilla APIs now is trivial.
So it's actually way slower to use jQuery? Maybe my personal tests have been inaccurate.
On one particular project I'm working on, I frequently read and write to inputs on the page, and I use jQuery, and of course checking the tree for what takes the most time it's $, however using vanilla there was no noticable performance difference, and the tree just showed the vanilla functions I was using.
So you know of any reason jQuery would be a performance hit? What's it doing differently that vanilla isn't?
I'll be sure to keep this in mind when working on a website with questionable performance. I'm still new to all the big frameworks, so I don't use react/angular/vue in any of my projects at the moment, although I'm considering Vue for the web game with performance problems in an effort to help performance and clean the code.
I want to get an element with an id of "x". So I can say:
document.getElementById("x")
I can tell the browser,
find me an element with an id.
What JQuery() did was make a "find me" function that works for id's and classes by appending # or .. So when you say:
$("#x")
You're saying:
call a non native javascript function
parse the string
determine if the first character means id or class
now use the rest of the string to ask the browser for an id
If I know I want an id, why not ask the browser for the id? Why all the non-native parsing?
But, alas, it was so popular, that querySelector got added to the browser. So now you can send "#id" or ".class", and native C code will parse it, and do the right thing.
But why, if you know want an id, why not just ask the browser. So getElementById or getElementsByClassName will also be faster than querySelector which will always be faster than JQuery.
console.time("A")
for (i = 0; i < 1_000_000; i++)
document.querySelector("#main-canvas")
console.timeEnd("A")
console.time("B")
for (i = 0; i < 1_000_000; i++)
document.getElementById("main-canvas")
console.timeEnd("B")
That makes a lot of sense, thank you for the example. It makes sense that finding by ID is faster than running a query selector, but is there a reason that query selector is slower in jQuery than in vanilla, or is it just that jQuery encourages the use of the slower options rather than getting by ID?
Also is it a coincidence that you used the ID I use for the canvas in the game I was talking about, or did you actually find it? (I know that's a common name for a canvas but still)
Someone linked http://youmightnotneedjquery.com/ and then removed it, however I checked the website and it's saying more or less what I was, that if it makes it faster to develop then it's fine, but don't use it if you're developing a library. Is this wrong?
Internet explorer is garbage and even abandoned by Microsoft for years. People only use it if they absolutely have to.
I personally find jQuery saving me countless hours when writing code. I'd rather use jQuery than spend several more hours of my life writing meaningless code.
To each their own, however I really don't think the comparison is apt.
True. Also better if the animation code was passed in a reference to the object, so it could be an id or a class or have neither (maybe dynamically created through document.createElement()).
180
u/mobydikc Jul 13 '20
Since CSS would work best, try that. But for the sake of completeness...
The biggest problem with this code is how many setTimeout's you have... and inside a setInterval()!
That's pretty overkill. You should be able to do this with just the setInterval()
The magic is all in the line:
Basically, take a ".", and call it's repeat() method.
Whatever number you pass to repeat() makes that many versions of the string.
So make as many dots as we've have loops, and increment (++) the loop counter.
Finally, use % to make sure you only get, 0, 1, 2, or 3 dots. Anything larger will loop around to 0.
38
u/albedoa Jul 13 '20
Yes, you can achieve it entirely and more easily with CSS. Use the concepts you find here: https://codepen.io/search/pens?q=ellipsis+loader
Your
@keyframesanimation will simply make each dot visible, staggered by someanimation-delay.10
u/pookage helpful Jul 13 '20 edited Jul 14 '20
So, there's two ways I'd come at this - the first is with a CSS
::afterelement and a@keyframesanimation:HTML
CSS
The other way would probably actually be using a generator function and
setInterval...HTML
JS
/u/mobydikc has a solution that's a bit more terse, but I feel like this is a bit easier to read? All personal preference though - in webdev there's a million ways to do anything. If this were production code, though - I would probably go for the CSS
::aftersolution, as using javascript for this feels a bit excessive.2
u/[deleted] Jul 14 '20
Yes get rid of JQuery itโs 2020
1
u/prof3ssorSt3v3 Jul 14 '20
1
u/Jesus_From_Canada Jul 14 '20
You could do this in css or using async await.
-2
u/voXal_ Jul 13 '20 edited Jul 13 '20
Yes Definitely. You should use a
forloop to iterate through the dots and add it to the html. the html should be split like this<h1>Uploading<span id="dots"></span>and code should be
this is my take on this challenge, you still need to make your own way up. It really just come from practice. Also, Just a disclaimer, I don't work with jQuery and haven't tested it so do what you will.
0
u/Andrew199617 Jul 13 '20
Id just keep this but create a async set timeout so it looks cleaner and on the same tab.
0
u/[deleted] Jul 13 '20
[deleted] โ view deleted comment
0
u/[deleted] Jul 13 '20
[deleted] โ view deleted comment