tags:

views:

37

answers:

2

i am making a chat with php ajax i want to show a message if the user has not typed any thing more than 1-2 minutes or event not navigating from one page to another.......if so than shows a message in the chat box [ last message recieved 2-3 mintus ago ] like in yahoo.......

+3  A: 

Use setInterval to periodically run a function that checks the value of the input, and compare it to the previous time you checked (which you would need to store).

If it hasn't changed, you can use an XHR to tell the server.

David Dorward
setTimeout would probably be better -- it sounds like he only wants something to happen when the text field has been empty for over 2 minutes after a message has been received. Just set a timeout when a message is received, and when it calls back, see if the textbox is empty
Carson Myers
+3  A: 

You would probably use setTimeout.

Set a timeout when a new message is received. You create a callback function that will be called after the time has expired -- the function would write the message. When a message is received, the timeout would be cleared as described in that link.

To expand on this, you might want to set some kind of variable in the script when the user changes the textbox, and have the callback function check the variable (which would be cleared when a new message is received) -- that way, if a user receives a message, and changes their mind and deletes it, the message would not be displayed. but I'm not sure if that's the behavior you want.

If you want the message to change every so often, use setInterval, which will first draw the message, and then update the amount of time that has passed since the message was received. You could even combine them -- set the timeout to 3 minutes, and then from the callback from setTimeout, start an interval for every 1 minute. You might want to clear the interval and erase the message when a new message is received though, so you don't have these stale meaningless "message received 5 minutes ago" messages lying around in the conversation.

Carson Myers