Looking for guidance on setting up an auto-updating counter with AJAX? Or perhaps you're curious about how to turn off the network

Can I create an auto-reload counter for messages using setTimeout(); to fetch JSON code from test_ajax.php?

Is it possible to send information from the server in this case?

I'm noticing a large number of requests being made to test_ajax.php when I check my Chrome network log (F12 -> network tab). However, websites like vk.com or facebook.com do not make constant requests unless there is a change.

What am I doing wrong with my solution?

UPDATE: Upon further investigation, vk.com sends requests to q%NUM%.queue.vk.com every 25s, but the last request remains "Pending" until then. When a new message arrives, it is displayed immediately. The requests have a "wait" parameter set to 25, which indicates a delay on the server side. How is this achieved?

Answer №1

Creating an Ajax counter is simple, just follow these steps:

  1. Include index.html
  2. Add counter.php (the ajax file)
  3. Ensure necessary images are included
  4. Add a JS file for jQuery paging call

You can download the required files from this link: https://docs.google.com/open?id=0B5dn0M5-kgfDcE0tOVBPMkg2bHc

Answer №2

Have you heard of COMET technology, sometimes referred to as Reverse AJAX? It might be what you're searching for.

An effective method for achieving your goal, such as conducting regular surveys, is one approach.

A significant amount of the action actually takes place on the server side; in order to prevent establishing new connections during each poll, certain servlet containers like Jetty have begun implementing methods like Continuation, which essentially keep a bidirectional connection open.

In the realm of Java, Servlet 3 includes asynchronous calls as part of its specifications.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to extract an array from a property value within an object using JavaScript?

Here's the code snippet under consideration: var structures = { loginStructure : function(){ return structure = [ '<form name="',opts.formClass,'" class="',opts.formClass,'" method="post" action=" ...

Exploring the Firebug console for JavaScript insights

Currently, I am searching for a method to retrieve the last command that was logged in the firebug console. As an illustration, I could implement something like: console.debug('The most recent request URI is /sweatsocks'); Following that, anot ...

To retrieve data from an AJAX response, you will receive an array in the form of a string

After requesting a list of posts submitted by users from my server, the response I received was a string containing an array of stdClass objects. If it had been an actual array, that would not have been an issue. However, it arrives as a string in the foll ...

Is it beneficial to use both Bootstrap and ng-bootstrap together?

I have two modules in my angular website - "bootstrap" and "ng-bootstrap". Do I need both or just one? I am thinking of keeping only "ng-bootstrap" 4.0.0.0 and removing "bootstrap". Is this acceptable? What are the steps to remove "Bootstrap"? Can I simp ...

Preventing the dispatch function from being overwritten by an empty state

Scenario: I am currently exploring the concept of React/Redux and aiming to understand the data flow. The process involves axios dispatching actions -> reducers setting state to props -> Component rendering. As a newcomer to the Frontend world, ...

Focus on React MUI Formik Input is lost when typing

I'm currently working on a form using React, MUI, and Formik, but I'm facing an issue where the input field loses focus after typing a character. I have wrapped each input field in a component for reusability and cleaner code, but it seems that t ...

Which is faster in JavaScript: using the IndexOf method or looping through an array?

I'm facing a situation where I have an array of JSON objects and need to identify the object with a specific property. While this may seem like a redundant question, please bear with me because my approach might differ from previous inquiries. A coll ...

Encountering a "TypeError: Cannot access the 'state' property of undefined" error while using React's "Map" function

It's clear from the examples below that the first photo functions perfectly when this.state.blabal is NOT located within the map(a, b){blabla} However, as seen in photo2, when I move the functioning block inside the map(a, b){`here!!`} {Object.k ...

Troubleshooting Guide: Issues with Bootstrap 3 Modal Window Implementation

This question is so simple that it's embarrassing. I attempted to copy the code from http://getbootstrap.com/javascript/#modals directly into a basic page setup, but it's not functioning. It seems like I'm making a very silly mistake. Here i ...

Problems encountered with the ajax contact form embedded in a WordPress theme

I have encountered an issue with a website I am trying to troubleshoot. The form on this particular page: appears to be malfunctioning... The form is failing validation and not sending any emails. The expected functionality is for the form to validate fi ...

How can an HTML5 application be configured to enable access to intranet and local files?

It's a known fact that accessing the local path of a file added using a file input field or drag-and-drop is not possible even with the new FileAPI. Whether this limitation is good, bad, or ugly is not up for debate. The FileAPI specs clearly state th ...

The custom confirmation popup is experiencing technical issues

Currently, I am utilizing a plugin to modify the appearance of the confirm popup. Although I have successfully customized the confirm popup, I am encountering an issue where upon clicking the delete icon, the custom confirm popup appears momentarily before ...

Can an older style class be inherited from an ECMAScript 6 class in JavaScript?

When I run the code below on Node.js version 4.2.1: 'use strict'; var util = require('util'); class MyClass { constructor(name) { this.name = name; } } function MyDerived() { MyClass.call(this, 'MyDerived'); } ...

Steps for adding an npm dependency as a peer dependency:

Is there a way in npm to install dependencies as peer-dependencies similar to the yarn option --yarn, rather than manually adding them? For instance: "peerDependencies": { "@angular/core": "^7.0.0" } Update: Further clarifi ...

Exploring the functionality of $scope.$watch in ES6

I'm encountering a problem while utilizing $scope.$watch in my ES6 project. The watch triggers once and then doesn't work thereafter. Below is the snippet of code: export class SomeController { constructor($log, $scope) { 'ngInject&a ...

Issue with Internet Explorer 9: Unable to receive successful response during Ajax request

Encountering issues in IE9 while trying to make an Ajax call to a REST API. Despite functioning correctly on other browsers like Chrome, FF (even in IE 10 & 11), the success response is not being received in IE 9. Instead, an 'Access Denied' erro ...

Exclude certain APIs from utilizing basic authentication in Fastify

I currently have two APIs set up: /auth and /no-auth. My goal is to only use basic authentication for one of them. To implement this, I am using the fastify-basic-auth plugin with fastify in a node environment. The /auth route should require authenticat ...

nodemon was not properly installed on the system

I attempted to add nodemon to my application using the command below: npm install nodemon -g This is the output that was generated: changed 116 packages, and audited 117 packages in 8s 16 packages are looking for funding run `npm fund` for details fou ...

What is the solution for handling nested promises in Node.js?

I am currently using the node-fetch module for making API calls. I have a function that handles all the API requests and returns both the status code and the response body. The issue I'm facing is that when returning an object with the response data, ...

Is there an improved method for toggling animations in CSS using jQuery compared to my current approach?

Looking to create a toggle effect for a dropdown box that appears and disappears when a button is clicked. var clickState = false; $("#show").on("click", function() { if (!clickState) { $(".animated").removeClass("off"); refreshElement($(".an ...