setTimeout in ajax

Hi is it okay to have setTimeout in ajax every 1 second or less than a second ?

Thank you in advance.

You can do it. But that doesn’t necessarily mean that you should do it.

Keep in mind, every time you use setTimeout or setInterval, whatever process it’s attached to will take up CPU resources.

So, even if it’s something very simple, causing the user’s computer to do something once every second (or less) takes focus away from something else.

Just a thought.

V/r,

:slight_smile:

Yes, it really depends on what you’re doing with it and how considerate of your visitors you are.

For example, if only checking to see if a variable’s value has changed it would likely be OK to have it run more often. But if you are parsing the entire DOM, not so much.

My rule of thumb is to avoid using it if at all possible, and if absolutely needed have it only run with as much delay between as would allow it to still work.

1 Like

Thank you for the reply, Actually I am having the project of GPS tracking system.I just wonder,…How other tracking system request the data to the server like theUber,that they can track in real time…

Thank you in advance.

For the most part, websites and webapplications are “stateless”, which means that the browser connects to the web server, requests documents, gets documents, DISconnects from the server, and parses the data into something viewable.

Things that update in real time generally are not stateless. There is some kind of tunnel keeping the connection alive, so that when new data is available it is “pushed” to the browser. You can see this on this forum. If someone sends you a private message while you are logged on, you will see a notification appear next to the word balloon in the upper-right section of these pages. (Or, I’m completely off my duff and it’s done another way that I am not aware of.)

HTH,

:slight_smile:

@WolfShade,

Yeah is see that if someone will send PM ,the ballon will pop-up,…I don’t know if using ajax can get the realtime…

Discourse uses a technique called long polling to update your browser with new notifications. An AJAX request with a long timeout value is made to the server, which holds the connection open for a set period of time while repeatedly checking for new notifications. If one is found, the server returns it to the browser and closes the connection, if not the connection eventually closes and the browser tries again.

The other alternative is websockets, which is what @WolfShade is talking about. It’s more efficient than long polling, but it’s not supported by older browsers, or PHP (out of the box).

2 Likes

Which, according to the last time I checked web browser metrics, shouldn’t be an issue. Most people are using Chrome (which should support websockets) or FireFox (ditto). And of the IE users in the world, a vast majority are using IE11 (don’t know if 11 supports websockets) or IE10. IE8 is only about 4%, and I have no idea about IE9, but I think a lot of users skipped over 9. I know a lot of companies who do web skipped IE9 support. WE sure did. :smile:

V/r,

:slight_smile:

1 Like

Unless you’re in a the corporate world. IE8 still accounts for 22% of my users. :smiley:

Bummer! Actually, where I am was IE8 until a few months ago. Then my section was “voluntold” that we would be the guinea pigs for IE10 rollout. If we had no or few issues, then the rest of the command would get IE10.

Everyone was switched to IE10 shortly before the end of last year… despite some of the issues we reported.

It hasn’t been real quiet, around here, for a while. :smile:

V/r,

:slight_smile:

Which is weird, because IE10 is in general one of the skipped versions. lol Kinda like IE7, but without the compatibility mode revert on others.

Nah… we skipped IE9 and went straight for IE10. IE11 is in testing/evaluation. If we get IE11, it will probably be in 2016, after it’s been poked, prodded, and approved.

:slight_smile:

We are still on 9 officially. Everyone I work with uses a modern browser like FF or Chrome though and IE for the few internal apps that require IE.

1 Like

@fretburner, What is the difference of websocket to the php socket ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.