I'm using WebIM for chat support on a website. I would like to be able to setup a timer for when a client initiates a chat session, if an operator/tech does not respond within 'x' seconds I would like the page to redirect to another page where the client can leave a message.

Sortof a 'please hold while we try to connect you' thing. That way if all the techs are too busy or helping other clients the client waiting can simply try back later or leave a message (as such when chat is offline).

I looked extensively over mibew.org the creators of the chat script, there is nothing regarding this feature, also it looks as though their website has just about been abandoned.

I have come up with an idea of using Javascript setTimeout function to run some php after 'x' amount of time. The php will basically query the DB to see if a tech has entered the session in question and if not then redirect the client to another page explaining that no one is available at the moment, but they can leave a message, etc.

Problem is, I don't have much of any experience with JS.

Is this a possibility? Is there another more effective/efficient was to accomplish this?

Perhaps this should go in the PHP section?

Recommended Answers

All 6 Replies

This worked for me:

<script>
var time = 150000;
setTimeout(function() { 
<!-- 
<?php include("libs/newchattimer.php"); ?>
// -->
if ('<?php echo $data;?>' == 'n') window.location.replace("http://daniweb.com");
}, time);
</script>

Thanks

I was wrong.... This code is NOT waiting 2.5 mins before it runs the PHP, it's running it immediately. How can I make it wait before running the PHP?

Hi Sabyre,

Problem is, I don't have much of any experience with JS.
Is this a possibility? Is there another more effective/efficient was to accomplish this?

Yes, I believe, you see php and javascript has interworked in one script file in the client side.

To make it clear, php are executed in the server, while jvascript is executed in the client side(your browser). Hope that clears it. :)

Anyway, here's a reference to get you up and running with JS.
MDN JAVASCRIPT

Thank you, that is a very helpful link. Reading through it I didn't see anything in it specifically regarding php within js.

I understand the fundomentals where php is server side and js being client side. I guess what i am trying to do is define a variable after x amount of time without delaying the rest of the php execution. I figured that being client side (js) it would simply request the value of $data from newchattimer.php after the time expires.

The above code does request the variable and if an operator does not answer the chat request it works as intended. However, it seems like it requests the variable immediately instead of after the timer expires, which is a problem if the operator DID answer the chat request.

This what I came up with and it works as intended. Thank you.

function opCheck()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {

   opCheck();                          
            //  alert('working2');                      
  }
}
opjoined = "newchattimer.php";
xmlhttp.open("GET",opjoined,true);
xmlhttp.send(null); 
}

function opResult()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {

   if (xmlhttp.responseText == 'n') window.location.replace("chatnoop.php");

 }
}
ajaxurl = "ajaxfiles/opAnswer_<?php echo $threadid;?>.txt";
xmlhttp.open("GET",ajaxurl,true);
xmlhttp.send(null);
}


setTimeout(function() {
    opCheck();
    opResult();
    //alert(op);

}, 150000);
clearTimeout();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.