简体   繁体   中英

How do I stop clients from sending ajax request to the server?

I have a web application in-which the client side sends an ajax request to a node.js server for an update request to check if there's any notifications for the clients user... This request happens every 30 seconds and even though i had performance tests on it my server side can't really take it. I have already updated the interval time to once every 2 minutes and yet clients who didn't refresh still sends the requests every 30 minutes. how can I make those clients take the update by demand and stop them form sending those requests?

Thanks in advance, Itay

Edit: Just to make it I have no problem limiting it and I already have. The problem is that the user doesn't refresh the page so the client-side code that includes that limit doesn't refresh on his browser. so Ajax vs web-sockets is not really my issue...

setinterval() function of javascript can be quite useful in your case.

You can control the frequency of a function execution by calling that function indirectly via setinterval() function that is configured via special interval parameter of your choice.function will then execute at that interval specified in milliseconds.

You can simply limit the ajax requests by invoking the function sending ajax requests indirectly via setinterval() function and limit the interval to 30 seconds.

hope this will be helpfull for you!!

please comment for more clarifications.

I think you should use web sockets in your NodeJS server to have a persistent but light connection with websites.

Check out Socket.IO, for example: http://socket.io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM