I have a ajax function implementation: when the user is on one site, the ajax call keeps checking the server every 5 seconds if there is a server url change. If there is, there will be a message displayed to direct the user to go to another site.

when the user go to another site, the ajax keeps checking every 10 seconds, if there is condition met, the message will be displayed to direct user to go back to the previous site. Also, after the message is displayed, ajax call keeps checking if there is another condition met, if there is, the message will automatically disappear.

The use case is: when one server is down, the message will direct the user to another site. On another site, when the previous server is up, the user could be redirected to the previous site. When the message is displayed (when server is down), the user may not follow the link to do anything. He/she may simply leave the message on and go to lunch or something, when he/she comes back, the server may be already up and the message should be disappeared instead of keeping showing the message. Therefore, the message should be displayed or disappeared automatically based on the ajax call condition.

I implemented the ajax function and it did check and displayed message. However, it wont' display the message only right after the user login or the user does a "refresh" to the page. After the message is displayed, when another condition met, the message won't disappear until the page is refreshed. Then the ajax calls keep doing every 10 seconds. The ajax call seems fine since when I set a alert, I did see the popup message show up every 10 seconds (but only after I refreshed the page.)

Now the problem is: the message can't be automatically displayed. It can only happen when there is new page load (refresh). How can I solve this problem. I'm thinking of putting "windows.location.reload;" to load the page right after the condition met. But kind of feel I don't need to do this. There should be better way to solve this problem.

Many thanks for your help!

sunnyday:

If the message is not displayed, you may not correctly implement your ajax for the display part. If you do the reload, you will need to solve other subsequence issues; besides, users may have problems working on the page -- the page is automatically refreshed. How do you implement your ajax to display the message? Alert? Display a div? Or else?

Hi! I was able to display the message (but only when the user login which is init the page load or when I refresh the page. ) The message is not popup, it was displayed in div which is a large section of HTML string with CSS style by appending the content into HTML/JSP page using the "id". The problem is it can do the display/disappear automatically while only when users login or refresh the page. I guess I need to put the ajax call function into a run-time function which can run all the time without triggering (like inti page load or onclick, etc.).

Yes, you need to have the ajax running all the time in the background. You should add in the onload; otherwise, you need to initiate it somewhere in your page as a callback with setTimeout() (no user require) in your body tag. I did it before. Though, your server will be very busy if there are many users using the page at the same time.

It didn't work this way. Besides, even if it were working, it will make the server very busy. Any other ideas?

Do you know how to use observe field with JavaScript? That may be the way to go.

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.