ajax clock encounters net::ERR_INSUFFICIENT_RESOURCES error

I recently set up an ajax php clock, but I keep encountering numerous net::ERR_INSUFFICIENT_RESOURCES errors in my console. Can anyone shed some light on why this might be happening?

Below is the code responsible for calling out the clock function:

$(document).ready(function(){  
    setInterval(function(){   
        $(".clock").load('<?php echo get_template_directory_uri(); ?>/melbourne.php');;
    });
}(), 1000);

This is what's included in the Melbourne.php file:

<?php  

date_default_timezone_set('Australia/Melbourne');

echo $date = date('H:i:s');

?>

Answer №1

It appears that you have placed the setInterval delay incorrectly. You mistakenly provided it within the $(document).ready() function, which may be discarding it. As a result, the interval is operating without any delay and continuously sending requests one after another.

Please see the correct implementation below:

$(document).ready(function(){
  setInterval(function(){
    $(".clock").load('<?php echo get_template_directory_uri(); ?>/melbourne.php');
  },1000);
});

Additionally, pay attention to the placement of parentheses after a function expression.

Consider learning about Immediately Invoked Function Expressions (IIFE) by visiting this link: IIFE.

function something(){
  // .. some code
}()

Event handlers require a function expression so they can execute it at a later time. Providing an IIFE will cause the event handler to receive the result of the function execution.

Answer №2

Instead of repeatedly making AJAX calls to Melbourne.php every second, which can drain resources and potentially hit request limits depending on the browser used, I recommend utilizing websockets for a more efficient method of communication. Websockets allow for full duplex mode communication with Melbourne.php, establishing a connection only once.

For more information on how to implement websockets, refer to https://socket.io/docs/v4/.

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

PHP enables real-time control of a pop-up message box

I am currently working on a real-time project and looking for a way to send live announcements or pop-up alerts that can be controlled by admins to all users on the webpage. Although I found a solution called Realtime announcer online, our company policy ...

Transfer files with POST data by utilizing the WebClient function

Is there a way to upload a file to a host using the WebClient class in C# and pass values to be displayed in the $_POST array on the server side (PHP) in one connection? Below is the code snippet I have tried: using (WebClient wc = new WebClient()) { ...

Having difficulty handling file data following the import of an Excel file in Laravel 5.8

I have a requirement to send multiple emails at once by importing an Excel file. Currently, I am successfully importing the Excel file and storing the data in a variable as an array. I am utilizing maatwebsite/excel version 3.1 for this task. However, afte ...

Guide on integrating buefy (a vue.js component library) into your Laravel blade template

I'm currently integrating buefy into my project, but I'm encountering issues with using vue.js on Laravel 5.8. Can anyone offer assistance? Here is the code snippet from my app.js: require('./bootstrap'); window.Vue = require('v ...

Is it feasible to utilize jQuery mobile without the use of AJAX, relying only on styles?

While I am aware of the jQuery Mobile download-customizer, I am struggling to determine which options need to be selected. It appears that the styles for input elements are somehow connected to the AJAX functionality? All I really desire is to have stylis ...

What is the best method for shifting the values in an array to the 3rd position from the

Can the position of values in the array be changed? I currently have an array structured like this: [files] => Array ( [name] => Array ( [0] => file 1 [1] => file 2 [2] ...

Building a table using jQuery and adding elements using JavaScript's append method

Greetings! I've been attempting to add new records from a form that registers or updates student information, but unfortunately it doesn't seem to be functioning correctly. Can anyone point me in the right direction as to why this may be happenin ...

Creative solutions for transferring information outside of Salesforce

Looking to streamline the process in Salesforce, we want to enable users to click on a quote button within an Account object record and seamlessly transfer specific fields information to either a web application or a windows application. Initially consider ...

Unable to reply via Gmail API as the Reply To address is not included in the headers. Response not

Utilizing the PHP language in combination with the Gmail API client to access a Gmail message and then reply to it. Upon examining the headers, there appears to be no "in-reply-to" field. Below is the code snippet I am using: $service = new \Google_ ...

Discover the method for tracking idle time with jQuery and PHP

I have a script that utilizes PHP sessions through a custom class. One of the methods in this class calculates the remaining seconds before the session expires. Whenever the user refreshes the page or opens a new one, the idle time counter is reset using ...

The webpage is not displaying any results despite using .innerHTML with Ajax

I am currently developing a web application that displays query results using the Google Books API. While my code is functioning, I am encountering an issue where the .innerHTML method does not show any results on the HTML page. Below is the HTML code bein ...

Getting the output from AJAX when the onreadystatechange event occurs

Struggling with retrieving a value from a function and storing it in a variable? Getting an "undefined" result due to JavaScript's asynchronous nature? Unsure how to fix this using "callbacks" or "promises"? Check out the code snippet below. The goal ...

Could Absolute Paths be the ultimate fix for problems with htaccess URL Rewrite?

Trying to accomplish a simple task of changing URLs from website.com/user?slug=usernameexample to website.com/user/usernameexample? I implemented the following code: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?us ...

Solving Issues with URL Parameters and AJAX

My JSP page has a JavaScript function called loadData() that is triggered when the page loads. This function makes an AJAX request to a servlet in order to retrieve data and return the necessary HTML content to display on the page. I am trying to call thi ...

What is the best way to retrieve the JSON data from a POST request made through AJAX to a PHP file and save it in an array variable?

My ajax request sends JSON data to a PHP file named 'receive.php'. user_name , user_id, etc. are defined at the beginning of my script but can be changed to anything else. Below is the JavaScript code I am using: const data = { name: user_na ...

The dropdown menu in the bootstrap is not being properly filled with content on the webpage

In my header file, I am attempting to add a bootstrap dropdown that will display the user id along with two other options in the dropdown menu. Within the header file, I have created a new div element for the dropdown and linked it to a php file where the ...

I have always wondered about the meaning of " + i + " in Javascript. Can you explain it to

<script> var x,xmlhttp,xmlDoc xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "cd_catalog.xml", false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; x = xmlDoc.getElementsByTagName("CD"); table="<tr><th>Artist</th><th>Ti ...

Struggling to retrieve the JSON information, but encountering no success

Here is the javascript code snippet: $.getJSON("validate_login.php", {username:$("#username").val(), password:$("#password").val()}, function(data){ alert("result: " + data.result); }); And here is the corresponding php code: <?ph ...

Attempting to acquire the PDF file using cURL, encountering a 404 error, whereas the browser successfully retrieves it with a 206 partial

I have been struggling to configure cURL to successfully download a specific PDF from a remote server. To gain more insight into the issue, I decided to install LiveHTTPHeaders. The output below shows a successful transfer through a browser. My question is ...

Leverage AJAX data to dynamically generate an input field within a Laravel application

. Hey everyone, I'm currently working on implementing ajax for a search functionality. The goal is to display links to the search results' pages along with checkboxes next to each result, allowing users to select orders for printing. Although I ...