Roy Harper
Coder
I created a notification system in my website. In order to get the latest notifications like someone places, paid, or cancelled the order, I created a function and put it inside the setInterval(). My question is, does it somehow affect the performance of the website as it constantly downloading a file? The only thing I am loading is the total number of new orders coming in.

[CODE lang="php" title="count_new_orders.php"]<?php
require_once('dbConfig.php');
try {
$sql = "SELECT * FROM orders WHERE isRead = 0";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
echo json_encode($total);
} catch (PDOException $e) {
die("Connection Failed: ". $e->getMessage());
}[/CODE]
[CODE lang="javascript" title="notification.js"]$(document).ready(function () {
countNewOrders();
function countNewOrders() {
$.ajax({
url: "../php/count_new_orders.php",
method: "post",
datatype: "json",
success: function (data) {
if (data == 0) {
$(".order-count").addClass("d-none");
} else {
$(".order-count").removeClass("d-none");
$(".order-count").text(data);
}
},
});
}
setInterval(() => {
countNewOrders();
}, 1000);
});[/CODE]

[CODE lang="php" title="count_new_orders.php"]<?php
require_once('dbConfig.php');
try {
$sql = "SELECT * FROM orders WHERE isRead = 0";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$total = $stmt->rowCount();
echo json_encode($total);
} catch (PDOException $e) {
die("Connection Failed: ". $e->getMessage());
}[/CODE]
[CODE lang="javascript" title="notification.js"]$(document).ready(function () {
countNewOrders();
function countNewOrders() {
$.ajax({
url: "../php/count_new_orders.php",
method: "post",
datatype: "json",
success: function (data) {
if (data == 0) {
$(".order-count").addClass("d-none");
} else {
$(".order-count").removeClass("d-none");
$(".order-count").text(data);
}
},
});
}
setInterval(() => {
countNewOrders();
}, 1000);
});[/CODE]