Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When I'm trying use function like sleep(2) in a new function I wrote, it's not working.
function deleterow(id){
$.post( "taskpost.php", { "deleteid": id, "delete":true } );
sleep(1);
$('#taskslist').load('taskslist.php').fadeIn('slow');
};
Have you coded sleep function or are you using it from some other library?
If you want to wait for the post request to complete try using this:
$.post("taskpost.php", { "deleteid": id, "delete":true } )
.done(function(data){
$('#taskslist').load('taskslist.php').fadeIn('slow');
});
If you still want to delay it somewhat you can use setTimeout inside done callback.
There is no such function as sleep() in Javascript. If you are attempting to add a delay to your function, use setTimeout(callback,delay) instead.
I think I know what the problem is. Maybe the post in jQuery isn't working. Sometimes when you have a function that can't be found/finished, it stops the previous function from continuing. The other problem is that there is no sleep function. If you are trying to delay, use the setInterval function.
Good luck!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I have a function (funcBig) that runs for a long time, Till it runs I want to repeatedly call another function (funcSmall) in parallel every few seconds. I want to implement it in Javascript but have no clue how to do it.
I can think of a way of doing it in python with something like this-
def funcBig():
{
#function body
}
funcSmall():
{
#function body
}
proc = Popen(funcBig()) #call funcBig in parallel
while(proc.poll() is None): #check if funcBig is still running
funcSmall() #call funcSmall repeatedly if funcBig is still running
sleep(10)
proc.wait()
How do I accomplish similar functionality in Javascrpit?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have some socket.io emits that I'd like to provide handling for when they are taking too long to get back from the server with their promise. But I also don't want to outright time them out. I have seen this on some websites where a loading spinner will add 'This seems to be taking longer than usual' text after 10 or so seconds.
How can I add a timeout-style method to a Javascript function that will do something after a certain amount of time, but will NOT cancel the original method?
Unless you have a global scope, you'll need to pass in two handlers
Something like this (I've generalised the flow because you've not provided enough information about your existing set up, so I hope it makes sense)
function doSocketRequest(normalHandler, tooLongHandler) {
let timer = null;
// set timeout to 5 seconds, if we hit it then trigger "tooLongHandler"
timer = setTimeout(tooLongHandler, 5000);
// When we receive our socket response for the event, clear the time and trigger "normalHandler"
socket.on('some_event_response', (response) => {
clearTimeout(timer);
normalHandler(response)
});
// Emit to server
socket.emit('some_event', ...etcetc);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i am trying to perform addition in javascript. below is my code :
function myfunc(size){
var m = parseInt(size)+5;
alert(m );
When i use 'alert',it works perfectly.. but i want to use sweet alert and it is not display anything via sweet alert. when i do:
function myfunc(size){
sweetAlert(size );
it does display the size using the code above. the problem is, it doesn't work when i try to perform the addition. why is it so?
try something like this
import swal from "sweetalert";
function myfunc(size) {
var m = parseInt(size) + 5;
swal(String(m));
}
myfunc("6");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am a javascript beginner, and I do not quite understand this code in the picture, can somebody explain a little ?
Thanks!!
The easiest way to explain the code is to fill in the values during each call.
The function plusGenerator is a function that takes an offset and then returns another function that returns the result of adding offset to another number. When you call plusGenerator(2), the function that is returned looks like:
var addTwo = function(x) { return x + 2; };
You then call addTwo(15) which returns 17.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If the title isn't clear enough, which is probably the case, here's what I mean :
Is it better to do this :
function example() {
if (condition) {
//The whole function code
}
}
or
function example() {
if (!condition) {
return;
}
//The whole function code
}
What is the cleanest way ?
Edit : I am not asking for your opinion. I'm wondering if there's any rule/convention/performance improvement.
I prefer the second way, "golden path" style.
Since I started to write all of my code like that I've found it to be a lot easier to read later. Perform checks in the order that makes sense, and return as soon as anything unexpected happens.
Wrapping code in a new block also adds indentation, which is kind of a waste of screen real-estate.