Jump to content

scenario question


squiblo

Recommended Posts

How could this be done, (what could I google, what could I look into).

 

There are two end users, person 1 clicks a button and an alert prompt pops up on his screen saying "you clicked the button" but also at the same time, person 2 recieves an alert prompt saying "person 1 click the button" and vice versa

Link to comment
Share on other sites

I believe the only way to do this is to store something on the server that notes who did what when, either in a file or a database, then have JavaScript that makes a periodic AJAX call checking for that information stored on the server. It won't be instant, but should be close. You'll have to find a good balance between browser performance and frequency of updates.

Link to comment
Share on other sites

what I'm actually trying to do is make a chat, I have it working at the moment and the ajax script gets the chat text from a file I call the function again. But there are two problems with doing this:

 

- cant click links in the chat

- can highlight anything (because it is a calling the chat so quickly)

 

so I only need to call the function when, the end user inputs text into the chat and when person x inputs text into the chat, instead of looping through it all the time. If you don't understand I'll try to explain again.

Link to comment
Share on other sites

Kinda. Before you update the users' chat boxes with new information, check to see if there are any differences between what they have and the chat.txt file. If they are the same, don't do anything.

 

Also, how often are you updating it?

Link to comment
Share on other sites

Show me your code. I could show you what I'm talking about if I had something to work with.

 

As far as MySQL, you could create a table that stored chat info. Have an auto-increment ID field, a timestamp field, a field to identify which user sent the message, and a text field for the chat text itself. Then store whatever the current ID is in a JavaScript variable, and like you said, check if there are any newer ones available.

Link to comment
Share on other sites

var xmlhttp1input;

function chat1()
{
showUser1();
}

function showUser1(str)
{
xmlhttp1input=GetXmlHttpObject1();
if (xmlhttp1input==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url1="http://localhost/includes/chat/chat_output/sql_output/sql1.php";
url1=url1+"?q="+str;
url1=url1+"&sid="+Math.random();
xmlhttp1input.onreadystatechange=stateChanged1;
xmlhttp1input.open("GET",url1,true);
xmlhttp1input.send(null);
}

function stateChanged1()
{
if (xmlhttp1input.readyState==4)
{
document.getElementById("chatoneoutput").innerHTML=xmlhttp1input.responseText + " ";
showUser1();

}
}

function GetXmlHttpObject1()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

 

This is the ajax that is getting the contents of a php file, in the php file there are queries and outputs of the chat. showUser1(); is what i put in to make it loop through infinite times.

Link to comment
Share on other sites

Try this:

var xmlhttp1input;

function chat1()
{
   showUser1();
}

function showUser1(str)
{
xmlhttp1input=GetXmlHttpObject1();
if (xmlhttp1input==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url1="http://localhost/includes/chat/chat_output/sql_output/sql1.php";
url1=url1+"?q="+str;
url1=url1+"&sid="+Math.random();
xmlhttp1input.onreadystatechange=stateChanged1;
xmlhttp1input.open("GET",url1,true);
xmlhttp1input.send(null);
}

function stateChanged1()
{
if (xmlhttp1input.readyState==4)
{
if (document.getElementById("chatoneoutput").innerHTML != xmlhttp1input.responseText){
  document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;
}
showUser1();

}
}

function GetXmlHttpObject1()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

Link to comment
Share on other sites

oh no sorry, I'm getting myself confused now, but the code I sent you seems to be doing the same thing you sent me back.

 

extra

And person 2 can only see what person 1 types because I am calling the first function at the end of the last function, which I dont want to do.

Link to comment
Share on other sites

Then you're gonna have to change to MySQL, which is the best option anyway.

 

To restart the process all over again without just calling it at the end of the function, change this:

function stateChanged1()
{
if (xmlhttp1input.readyState==4)
{
if (document.getElementById("chatoneoutput").innerHTML != xmlhttp1input.responseText){
  document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;
}
showUser1();

}
}

to this:

function stateChanged1()
{
if (xmlhttp1input.readyState==4)
{
if (document.getElementById("chatoneoutput").innerHTML != xmlhttp1input.responseText){
  document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;
}
setTimeout("showUser1()", 500);

}
}

 

That will cause a half-second (500 ms) delay.

Link to comment
Share on other sites

Delaying the function call only reduces my problem a little, because when I highlight the text I can only highlight it for 500ms.

 

I think I have a solution if you could please make these two functions into one function. With the 1 big function I will try this.

 

- get response text

- if response text = document.getElementById("chatoneoutput").innerHTML;

- loop through again

 

- if they are not equal go to a separate function that is not in the "loop"

- and then put "document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;"

 

this way the whole stript is not being looped through, only a section of it is.

 

function showUser1(str)
{
xmlhttp1input=GetXmlHttpObject1();
if (xmlhttp1input==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url1="http://localhost/includes/chat/chat_output/sql_output/sql1.php";
url1=url1+"?q="+str;
url1=url1+"&sid="+Math.random();
xmlhttp1input.onreadystatechange=stateChanged1;
xmlhttp1input.open("GET",url1,true);
xmlhttp1input.send(null);
}

function stateChanged1()
{
if (xmlhttp1input.readyState==4)
{
if (document.getElementById("chatoneoutput").innerHTML != xmlhttp1input.responseText){
  document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;
}
showUser1();

}
}

Link to comment
Share on other sites

function showUser1(str)
{
xmlhttp1input=GetXmlHttpObject1();
if (xmlhttp1input==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url1="http://localhost/includes/chat/chat_output/sql_output/sql1.php";
url1=url1+"?q="+str;
url1=url1+"&sid="+Math.random();
xmlhttp1input.onreadystatechange=stateChanged1;
xmlhttp1input.open("GET",url1,true);
xmlhttp1input.send(null);
xmlhttp1input.onreadystatechange = function(){
if (xmlhttp1input.readyState==4)
{
if (document.getElementById("chatoneoutput").innerHTML != xmlhttp1input.responseText){
  document.getElementById("chatoneoutput").innerHTML = xmlhttp1input.responseText;
}
showUser1();

}
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.