Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I was trying to ping a target like 10 times after the user clicks the button with a small amount of delay.

I could do it with a for loop, but the problem is that the document would freeze while it does executes everything in the document, and I want to echo it out as each ping is done.

I've tried multiple methods now and ran into problems everytime. I guess the best solution would be to use ajax and then loop it within the javascript, or maybe just execute the code several times. I've not however been able to figure out how to do it.

This is what the function looks like.

function ping($host, $port, $timeout) {
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms";  
}

And I use this to call the function in PHP.

echo ping($website, 80, 10);

So, I basicly added it to the document where the function is located.

Now, I've been trying to figuring out how to load this for a while, and it hasn't gone to great. Would love any help with this.

How do I load and use this function without freezing the document completely untill it is done?

share|improve this question
    
What exactly is your question? – simshaun Jul 13 '13 at 6:14
    
Actual question: How do I load and use this function without freezing the document completely untill it is done? – user2578535 Jul 13 '13 at 6:28
    
You want to make an asynchronous request in JavaScript. jQuery.ajax is asynchronous by default, so you shouldn't encounter any blocking if you are using it. – simshaun Jul 13 '13 at 6:34
    
I'm asking for more of a snippet of code which I can use since I've googled like hell so far – user2578535 Jul 13 '13 at 6:40

checkout phpbuilder.com, it will get you started and they're pretty gentle on noobs.

You don't want to call this code 10 times over Ajax, it wont be beneficial for what your trying to do because it will cause 10 different requests. It would be better to call it 10 times in a loop. additionally, you should change your ping to send some form of data after it connects and then time the response. Your code above will time the connect but that's it.

You can send a bogus request which will result in a 404 error, but that is a minimal response rather than a full page. Time how long from the time you send the request to the time it takes to get the response and this will give you a more realistic ping estimate.

Give a better description of your problem, like how it doesn't work, and someone will be able to give you a better answer

share|improve this answer
    
That was a insanely dumb comment. I didn't ask for a tutorial website, I asked for a piece of code or somewhere where there was a tutorial that is directly relevant to this. – user2578535 Jul 13 '13 at 6:05
    
I'll except blunt, but not insanely dumb. You sound like you need a little more than code. This isn't a good site for a 1 on 1 walk though, it's a Q/A. And no offense was meant by the noob comment, we've all been called noobs at some point. – JSON Jul 13 '13 at 6:18
    
Although I wouldn't call myself a noob. I've just not used JS too much. I didn't ask for help with the PHP mate. If you want to help, link to a tutorial that helps me, don't link a random website that doesn't help at all. – user2578535 Jul 13 '13 at 6:26
    
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. – liyakat Jul 13 '13 at 6:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.