My datepicker is not working that well - javascript

Hi my friendly computer wizards!
I coded a simple datepicker in jQuery, the already finished packages was to heavy for my taste. I thought everything was a-ok until I noticed strange behaviour. I first decided it might have been the alcohol messing up the latency between my eyes and my brain, but when, after waking up, the problem persisted I decided to find help.
When I repeatedly click one of the arrows to change month, the script
starts to lagg behind and eventually stalls. After a while it comes
back to life. Approximately the first four or five month-changes work
as intended.
I have been trying to simplify the Date() calls to the best of my limited knowledge, but to no avail. Do I have some kind of memory leak?
I fiddled up an example that replicates the problem here (complete code): http://jsfiddle.net/DCfMj/
Any help is appreciated. It is also okay to say that I have written immensly crappy code and that I am a worthless sack of blood because of it. Constructive criticism like that builds character.

Every time you call $(...).datepicker, you're adding another click handler.
Eventually, it starts taking a long time to run all of those handlers.

Related

Making a countdown clock that resets and changes the page content

I'm slowly learning to implement Javascript into my websites to generate automated changes, actual code is scary for my graphic designer approach but I think I'm getting there.
I followed an exercise some time ago that had me make a countdown clock that resets every time it reaches 0, nothing to crazy but it got me thinking:
Is there any way to make that reset affect the content of the entire page?
The practical case that made me think of it is the website of a restaurant that has a different coupon every day of the week, they upload the coupon code to their social media everyday but if I could develop a site that loops the codes depending on the day and make it change the text content and the stylesheet on it's own the process would be automatic.
I can't figure out how you'd link the clock reset to that change though, tried searching for it and I think this might be solved with some AJAX shenanigans, but it seems to be a bit too specific to find, any guidance would be greatly appreciated.
Hi and welcome to stackoverflow. This is a very general question. It is hard to answer without specificity that requires knowledge the community doesn't have. For example:
When you say "I followed an exercise some time ago...", what did the code look like? What was your final product? How is JavaScript used?
When asking a question in stackoverflow, it can be helpful to share a snippet of what the code looks like so that others can help by testing the code themselves and giving feedback. Or at the very least observing the functionality and maybe catching small syntax errors.
To the second point of "Is there any way to make that reset affect the content of the entire page?" Yes. There are MANY MANY MANY ways to affect content of a page, either in pieces or in its entirety. Once again it depends on what you want to do with the code that YOU have specifically. Even with the basic tools of HTML and JavaScript you can do this. You don't need "AJAX shenanigans" LOL.
If you are just starting out, dig deep into the basics of JavaScript(if that is your preferred language). You will find a whole host of versatile functionality through creating objects, methods, manipulating the DOM, building your own event handlers, etc. Also if none of what I just wrote makes any sense, then you now have some terms worth researching on your own.
I hope that this is helpful for you, and I wish you the best of luck on your coding journey.

Stopping Node.js endless loop from on html button click

is it somehow possible to stop a long computation (in this example an endless loop) on a node.js webserver from HTML?
At the moment, I have two buttons (start and stop). The start button emits the function started on button click as follows:
function started(){
socket.emit('started');
}
and on the server.js:
client.on('started',function(){
while(!cancel)
{
}
});
How can I exit the loop from clicking the stop button?
Well, you don't really show us enough code, but you probably can't do this. node.js is single-threaded event driven system. As long as you are in the middle of a while loop, you can't get any more events so you can't ever process anything that from the client that would change the cancel variable.
The only way this could work (though it's probably still undesirable coding on the server) is if your own server code inside the loop could change the cancel variable without getting any new events from the outside world to cause the loop to stop.
If you're relying on some other event to come into the server to change that flag, this could never work. The whole design also appears to imply that a server is only serving the needs of one user which is also likely an improper design for a server.
This particular question as posted right now is an XY problem, where you failed to explain your overall problem you're trying to solve, but instead described some issue you ran into in your particular attempted solution. That prevents us from helping you with the real problem and, in this case, all we can really tell you is that this is a wrong solution and will not work. Please don't post XY problems. Tell us your real problem. It's perfectly fine to show us your attempted solution and what issues you ran into with it, but only after you've explained the overall problem you're trying to solve. That allows us to help you with higher level and better solutions that you haven't even thought of to ask about yet.
We could only help with a proper solution to your problem if you explained the actual top level problem and showed us a bunch more code.

Website mobile switchers

Not long ago I got my hands on a website, which I played with a bit. Mostly trying to implement a mobile switcher from mobile view to desktop view and vice versa.
I came accross some really "fancy" javascript switchers, that did the job, but i could say it was buggy.
I coded a rather simply switcher in JS a while ago, which worked fine, but was waaaay too simple from the one that was "fancy".
Not long ago I managed to code a yet simple switcher in PHP.
And i havent stumbled upon a lot of PHP mobile switchers.
Was just wondering whats the difference between a very fancy one and a simple one, and between javasctipt switchers and PHP. Which one is better?( in one way or another)
(Worth mentioning that at the end everything modified viewport tag)
and that I took PHP and JS rather in an amateur way (never had it professionally taught) so I might be missing a lot of things here and there.
Well, since I didnt get much help here, I figured out myself that a php mobile switcher seems more efficient than one written in js.
Basically as far as I checked, it would be better if everything was done on server side, rather than have the visitor do it. This ofc increases the work server has to do, but I believe is too small to be taken into consideration.

a script with two different behavior on the same version of chrome

I'm not familiar with javascript but my boss would like me to solve something weird so I took some time to investigate, with no satisfaying answer so far. Problem is, we have a script (jQuery) which works perfectly on most of our computers except for two of them, on chrome exclusively. All with the exact same version of the browser (55.0.2883.87 m). The script is a slider (Flipster) with 4 slides, with 3 hidden on the faulty machines instead of 4 with one bigger on focus when everything goes according to plan. Anyway, you obviously won't be able to find the answer for me (and I don't ask for it) but what I may ask you is where I might start digging, if some of you ever faced such situation and found their way through this in some particular areas...
Thank you for your attention!

Random Quote Generator Dilemma

My code works fine on my pen at Codepen... Random Quote Generator at Codepen
BUT...
I've been going over this at my site, but I'm just not seeing what the issue is. Random QuoteGenerator at my personal site
var quote = document.getElementById('quote');
var click = document.getElementById('click');
var quoteList = ["'You will never be happy if you continue to search for what happiness consists of. You will never live if you are looking for the meaning of life.'",
"'Do not read, as children do, to amuse yourself, or like the ambitious, for the purpose of instruction. No, read in order to live.'",
"'Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.'",
"'A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.'",
"'In vain have I struggled. It will not do. My feelings will not be repressed. You must allow me to tell you how ardently I admire and love you.'",
"'It is better to remain silent at the risk of being thought a fool, than to talk and remove all doubt of it.'",
"'Have you ever been in love? Horrible isn't it? It makes you so vulnerable. It opens your chest and it opens up your heart and it means that someone can get inside you and mess you up.'",
"'The problem with the world is that the intelligent people are full of doubts, while the stupid ones are full of confidence.'",
"'Ok. You fuck me, then snub me. You love me, you hate me. You show me a sensitive side, then you turn into a total asshole. Is this a pretty accurate description of our relationship.'",
"'I love sleep. My life has the tendency to fall apart when I'm awake, you know?.'",
"'If I had a flower for every time I thought of you...I could walk through my garden forever.'",
"'Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great.'",
"'Never tell the truth to people who are not worthy of it.'",
"'The one you love and the one who loves you are never, ever the same person.'",
"'Becoming fearless isn't the point. That's impossible. It's learning how to control your fear, and how to be free from it.'",
"'I hope she'll be a fool -- that's the best thing a girl can be in this world, a beautiful little fool.'"];
var randomQuote = function(){
var twitter = document.getElementById('twitter');
var index = Math.floor(Math.random()*quoteList.length);
twitter.href = "https://twitter.com/intent/tweet?text=" + encodeURI(quoteList[index]);
quote.innerHTML = quoteList[index];
};
randomQuote();
click.addEventListener('click', randomQuote, false);
So the question is, what is going on? Same code, quotes aren't working on my personal site. Any help would be greatly appreciated. Thanks.
In your HTML script element, you're referencing the script with a href attribute when it should be src.
Also your code is executing before the DOM exists, move the Javascript to the end of the HTML body section.
<script src="//six03.com/quotes/script.js" type="text/javascript"></script>
</body>
</html>
You're attempting to bind the events prior to the page finishing its load. The simplest fix is to just move the script tag to the bottom of the page and load it at the end of the body.
Otherwise you can use a library to be sure the page is loaded or bind to an event. In jQuery that would be using the $(document).ready(function(){}) event.

Categories