0

I'm trying to run a function in JQuery that basically shuts down or starts up a server. The code I have so far is this -

$(".stopServer").click(function(){
    $.post("controller.php",{stop: 'true', server: this.name});
    $('#test'+this.name).load('controller.php?status=true&server='+this.name);
});

The problem is obviously it stops the server fine but it updates the status div ('#test'+this.name) straight away. This is no good because the server takes a period of time to shut down. I've been trying to get SetTimeout to work but can't figure it out... Any help would be appreciated.

Thanks guys, you're the best :)

UPDATE:

Full functions are here:

$(document).ready(function() {

$(".startServer").click(function(){
    $.post("controller.php",{server: this.name});
    setTimeout("showStatus('"+this.name+"')", 3000);
});

$(".stopServer").click(function(){
    $.post("controller.php",{stop: 'true', server: this.name});
    setTimeout("showStatus('"+this.name+"')", 3000);
});

function showStatus(name) {
    alert(name);
    $('#test'+name).load('controller.php?status=true&server='+name);
}
});

UPDATE

Given up on the idea of it, instead the status is polled for every second instead.

var refreshId = setInterval(function()
{
    $('.status').each(function() {
    var $name = $(this).attr('name');
    $(this).load("controller.php?status=true&server=" + $name);

});
}, 1000);

3 Answers 3

2

I've added a quick sample of wrapping the function in a setTimeout

​$(document).ready(function(){
    $('#test').click(function(){
        var message = 'hello';
        setTimeout(function(){ callback(message) },1000); 
    });
    function callback(name){
        alert(name);
    }        
});​

JSFiddle DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

I've given up, I just poll for the status every second instead.
@Steve - cool; well here is demo if you decide to change your mind! goodluck!
Does what I wanted anyways, so I'll give you the chosen answer :) Thanks buddy :)
2

I dont know if you will get a response from 'controller.php' when the server actually shuts down, in case you don't, try this...

$(".stopServer").click(function(){
    $.post("controller.php",{stop: 'true', server: this.name});
    setTimeout("showStatus('"+this.name+"')", 10000);
});

function showStatus(name) {
    $command = $('#test'+name).load('controller.php?status=true&server='+name);
}

6 Comments

@Steve, this is probably your best bet for what you're trying to do. The 10000 means 10 seconds delay and he is passing a function as a parameter.
This is definetly what I'm trying to achieve, but it isn't working... It's running the shutdown command, but not updating the div.
Try debugging it to see what is going on, if you are using chrome or firefox use the developer tools and use breakpoints to see what is actually happening :)
Yeah I'm trying, I can get it to run like this - setTimeout(showStatus(this.name), 5000); - but the delay doesn't work.
@Steve that will run it instantly; you might need to wrap the above answer in this: setTimeout(function(){ showStatus(this.name)}, 10000);
|
1

ajax calls are asynchronous. the $.post() call returns immediately and lets the actual post work be done in the background. either change it to a synchronous call (usually not a good idea), or put the subsequent code in the "success" part of the .post call, e.g.

$.post('controller.php', success: function(data) {
    $command = etc....
});

4 Comments

Don't think the server would've shutdown by the time the post completes; guess thats why he's asking for a setTimeout or interval solution.
Yeah the server takes 3 seconds to shut down, the command to tell it to do that returns after like, milliseconds...
at least the JS stuff will wait for the server-side script to return, instead of just zooming on to the next line of code. I'd put the delay into the success handler as well, so the 3 second count starts AFTER you've gotten confirmation the shutdown command was issued.
I don't know how to do the delay. I'm not concerned about it zooming into the next line of code, it literally just sends the word "stop" to a console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.