Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How does setInterval() affect the website's performance?

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.

1630643015510.png


[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]
 
Solution
This might not neccessarily hurt the browser / user experience, but it can affect your server.
Solutions like this work great to get a web app up and running quickly, but they're hard to scale. Imagine having hundreds of thousands of users, or millions, all logged in and requesting data from your PHP files using AJAX. It gets out of hand quickly.

However, you can definitely do something like this for a while and before anyone notices... just be prepared to eventually convert it to something more lightweight (for your server). Some of the most recommended setups use web sockets so that users can be notified of new events (notifications) as they happen without having to constantly check with AJAX / PHP. It's more efficient, faster, and...
This might not neccessarily hurt the browser / user experience, but it can affect your server.
Solutions like this work great to get a web app up and running quickly, but they're hard to scale. Imagine having hundreds of thousands of users, or millions, all logged in and requesting data from your PHP files using AJAX. It gets out of hand quickly.

However, you can definitely do something like this for a while and before anyone notices... just be prepared to eventually convert it to something more lightweight (for your server). Some of the most recommended setups use web sockets so that users can be notified of new events (notifications) as they happen without having to constantly check with AJAX / PHP. It's more efficient, faster, and scalable. At that point you end up using a lot more JS and less and less PHP - Or no PHP at all. A Node.js install is one of my favorite ways :)
 
Solution
This might not neccessarily hurt the browser / user experience, but it can affect your server.
Solutions like this work great to get a web app up and running quickly, but they're hard to scale. Imagine having hundreds of thousands of users, or millions, all logged in and requesting data from your PHP files using AJAX. It gets out of hand quickly.

However, you can definitely do something like this for a while and before anyone notices... just be prepared to eventually convert it to something more lightweight (for your server). Some of the most recommended setups use web sockets so that users can be notified of new events (notifications) as they happen without having to constantly check with AJAX / PHP. It's more efficient, faster, and scalable. At that point you end up using a lot more JS and less and less PHP - Or no PHP at all. A Node.js install is one of my favorite ways :)
Thanks for pointing out how it can affect the server, I have not think of that. On the contrary, I am just using this code on admin site, which means only one user is requesting this data. Do you think it'll be okay?

Regardless, Web Sockets and node.js seems like a way to go. I'll be studying that.
 
Thanks for pointing out how it can affect the server, I have not think of that. On the contrary, I am just using this code on admin site, which means only one user is requesting this data. Do you think it'll be okay?

Regardless, Web Sockets and node.js seems like a way to go. I'll be studying that.
If it just one user then you will be completely fine. One thing I like to do though is detect activity so that if you leave the computer for a while, it stops auto updating and then updates when you return.
 
Can someone please explain to me what is dynamic data type in java script?
A dynamic data type in JavaScript (or others) means that the variable can be of any type.
What types? Well, we have "strings", integers like 1, 2, 3 and floats (decimals) 1.2, 2.5, 3.1, and other formats too... [ ] , { } , etc

Code:
let somevar = 2;
let somevar = "hello";
let somevar = 5.7;
let somevar = [2, 5.7, "hello"]

There is nothing actually forcing the variable to remain the same type.
In other types, a non-dynamic data type (if integer, it can't be given a non-integer value)
 
A dynamic data type in JavaScript (or others) means that the variable can be of any type.
What types? Well, we have "strings", integers like 1, 2, 3 and floats (decimals) 1.2, 2.5, 3.1, and other formats too... [ ] , { } , etc

Code:
let somevar = 2;
let somevar = "hello";
let somevar = 5.7;
let somevar = [2, 5.7, "hello"]

There is nothing actually forcing the variable to remain the same type.
In other types, a non-dynamic data type (if integer, it can't be given a non-integer value)
In this code what will be the output then?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom