Stopping Node.js endless loop from on html button click - javascript

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.

Related

How to write a twitter-like RichText box for mentioning entities?

After weeks of trying and testing to find a solution for my needs I admit that I still have no idea how I can solve this problem.
It sounds simple: I want that a user is able to mention things in a text area similar to twitter.
The problem is that I can't seem to manage it to make it work. Every browser has its own specialties which are coming into my way and break things. I have tried multiple different attempts but none of them worked even on a single browser completely.. mixing text and HTML appears to be incredibly hard to do.
So here I am. Asking you guys for any kind of help. Whether it's a library you can recommend me that is already doing what I need here, or if you did something similar and can tell me what exactly you did to make this work on multiply browsers.
My current solution looks something like this: Hitting # will insert a input text field into a div contenteditable everything is working nice so far unless the whole thing is the first element of a row. If the caret is also at position 0 and the user hits Enter, then something dies inside the browser which removes the whole input box without further notice or any events - at least not on Chrome. That was the most promising solution that I was able to come up with. Don't think I didn't try to save it by inserting e.g. a native Text with a zero-whitespace-character but that doesn't work either. It works better - but not completely.
I'm really frustrated by now and this is holding my whole project back which has this key feature that has to work properly - mainly because the information put there is going to be persisted as XML but that is a completely different story.
I really hope somebody can help me to get a solution for this. Bear in mind that I am actually using GWT 2.8.0 but I would not mind to use/wrap a JavaScript library at this point ..

How to debug a jumpy web UI

I have a question about debugging procedure; I'm having a problem with a scrollable div that jumps to the top of its content every time that I receive a response from the server. This was initially just a nuisance, but now it's causing a Select2 widget of mine to display incorrectly when it receives its results from the server.
I'm using a proprietary, homegrown client-server architecture developed within my company, and based off of jQuery on the client-side. The codebase is massive, it has a number of side effects that occur when information is received, and I have no idea where to even look to find what code is causing this.
So I'm wondering if anyone here has any creative ideas on how I can debug this issue. So far I have tried commenting out the code that I guessed was causing the issue (apparently it wasn't), as well as tracing the code starting from the receipt of data from the server.
I found the bug using a form of divide and conquer (thanks #Kevin Brown for the link). Effectively I placed a break point at a central loop in the software, and as the loop progressed, I was able to see visually the point at which the UI jumped. Once I had the particular iteration determined (by console.log()), I was able to further narrow down the source of the bug.

TinyMCE remains hidden (uvw-dialog-open)

Ok i've developed a nice tinymce-solution, where i create and destroy all the tinymce-instances programmatically through js - so, i know it is maybe not the daily-usage of a tinymce implementation - but basically it works like a charm.
Now, before i will give you specific example code - i will explain my strange issue: A friend of me is an extreme power-user of the online-tool i made and he is creating/destroying hundrets of tinymce-instances during the day...
Sometimes, after hours of work, he has the behavior, that tinymce won't show up when he hit "edit"-button. I never made it, to reproduce that on my own - but one day, in a teamviewer-session, i was able to have a look into his screen and page (with firebug), when the error already happened.
So, badly i was not able to make a full debugging through the javascript-code (because when you hit one times f5 in this situation, the error disappears and it will take some other hours to get it again) -> i realized, that, when the error was happened -> everything in the tinyMCE-object itself seems ok -> also everything in the DOM-rendering seems ok -> BUT, from some strange css-import-file, there was suddenly a definition like this:
html.uvw-dialog-open object, html.uvw-dialog-open iframe, html.uvw-dialog-open embed {
visibility: hidden;
}
This is causing that a main-panel of tinymce won't show and nothing of tinymce is visible anymore.. killing and recreating of the instance won't fix the bug in this moment, you must press f5 and after a reload, you even can't find this css-definition again (or, at least, i was not able over teamviewer and his shitty, small laptop)
So, the only thing that came in mind was an ugly hack in my own css, telling this:
html.uvw-dialog-open object, html.uvw-dialog-open iframe, html.uvw-dialog-open embed {
visibility: visible !important;
}
And, since then... it was quite for weeks -> but today, my friend calls me again, telling me, that he can't see tinymce, AGAIN.. i was almost in tears, you can imagine :D
Ok.. after writing and re-reading all these lines -> i realize that my fix won't work... both are the same definitions and if they appear on the same level (file, not inline).. probably last-match-wins i guess, what would be the newly, lazy loaded tinymce-file.. so it will definitly be better, to make an inline visibility:visible; over the init_instance_callback of tinymce...
But, in my desperation, i thought i will write everything down here on stackoverflow -> maybe someone knows the real cause of this issue and.. you guys are the most awesome devs out there i know :D
Jebbie,
Thoughts more than a definitive answer ...
Sounds like a memory leak issue due to lots of javascript/DOM activity in a long-lifed page.
You've probably done nothing wrong and you're unlikely to track down the actual cause, however certain measures are available to you :
Try making your tinymce instances reusable rather that destroying and creating new every time.
Periodically have the page request a re-serve - it may be a challenge to reproduce the entire document state - DOM and javascript environment - in the re-served page.
Lots of work and no guarantees I'm afriad.

Pausing/Breaking execution in javascript for debugging, but only for certain javascript files

Do any browsers support pausing execution / stepping through code, but only for certain files or regions? I'm using jQuery and several other libraries, and I'm not interested in stepping through their code in 99% of cases, as it is my code that is wrong. All my code is in my script file, other libraries are included separately.
If I use the pause button, I tend to find that I am very quickly taken to some part of jquery with code I don't understand or need to understand. If I click the step into button a lot, I sometimes get taken to a point in my script, sometimes not.
I could manually click every single line of my code to add a breakpoint, but this would be extremely tedious, and I couldn't un-pause execution easily. If there is a browser that lets you select multiple lines and add breakpoints to all of them in one click, that would be a great alternative, though.
There are some new features in the Chrome developer tools that can help you get a good entry point into a new code base. Event listener breakpoints will let you pause execution at a given event handler:
Things have changed since I asked the question 5 years ago. Chrome (and possibly other browsers) have an option "Blackbox script". If you find yourself paused inside a library, e.g. jQuery, right click, choose "Blackbox script" and you will automatically skip over all lines in that file when you are stepping through the code in future.
This is what step in/over/out is for. If the program is stopped for debugging, and you're stepping through and see that you're about to descend into a jQuery function (or anything else you want to skip over) you click "step over". If you got into something which you want to get right back out of, you click "step out".
Simply set the breakpoint where you want to begin debugging, and use the step functions from there to control what you're stopping to take a look at.
Put the break points in the desired javascript files and where you want to inspect your code!
Press this will let you skip the jumping to other files and you will be able to continue in the same JS file

LinkButton sometimes requires two clicks

I have an ASP.NET 4.0 Web Forms app and I'm using jQuery to do most of the JavaScript. The problem I'm having is that some of the links sometimes require two clicks for them to do anything.
Seemingly this happens at random and I have been unsuccessful in reproducing the problem in any kind of repeatable fashion, also there don't seem to be any JavaScript errors, so that's probably not the issue.
I'm at a loss as to what might be causing the issue. What might it be?
Without a view of your Javascript, HTML or backend code, there's not much I can suggest as to what might be causing the problem.
However, if you have Firebug installed, you could use that to see what clicking the button is doing each time and go from there.
Also... what is it supposed to do? What does it do the first time? These are questions that will need answers before debug assistance can be provided

Categories