'How to refresh API url in jQuery?
I have create small order food Application, I need get new order then automatically updated order data in popup working fine, now I want to get data without page refresh new order data, also am using setInterval method, but every 5 second data updated page speed down for reason every second 2. see my code could you please solve this issue give any another idea.
my code:
sendRequest();
function sendRequest() {
var token_id = getUrlParameter("id");
var datas = {
"admin_id": token_id
};
var closedJson = JSON.stringify(datas);
$.ajax({
type: "POST",
url: "https://demoapp/api/v6/manager/takeaway/new-orders",
async: false,
data: closedJson,
contentType: 'application/json',
dataType: 'json',
success: function(data) {
// $('.feedback-box').html(data); //insert text of test.php into your div
console.log(data.new_orders)
var News1 = data.new_orders;
var orderItems = data.new_orders;
var orderDate = "";
var newOtp = "";
var aedNew = "";
var order3 = "";
var orderID = "";
var quantity = "";
var variation = "";
var orderInfo = News1.map(function(x) {
orderDate += "<p><b>Date & Time</b></p><p class='newDate'>" + x.summary.order_date + " " + x.summary.order_time + "</p>";
// alert(x.summary.order_date)
});
var orderInfo1 = News1.map(function(x) {
newOtp += "<p><b>OTP</b></p><p class='newDate'>" + x.summary.otp + "</p>";
quantity += "<span>item " + x.summary.item_count + "</span>";
var order_IDs = x.summary.order_id;
orderIDs(order_IDs);
});
var orderInfo2 = orderItems.map(function(x) {
aedNew += "<span>" + x.summary.order_amount + "</span>";
});
var ordervariations = orderItems.map(function(x) {
x.items.map(function(x) {
variation += "<p class='new-order-name'><span class='pull-left'>" + x.item_name + "</span><span class='pull-right'>X" + x.quantity + "</span></p>";
x.variations.map(function(x) {
variation += "<p><span><i><b>" + x.variation_key + "</b></i></span>:<span>" + x.variation_value + "</span></p>";
});
});
});
var orderInfo3 = News1.map(function(x) {
order3 += "<p class='new-user'><img src='images/user.png'>" + x.user.username + "</p><p><img src='images/phone.png'>" + x.user.mobile_number + "</p>";
orderID += "<span>Order " + x.summary.order_no + "</span>";
});
$('.newOtp').html(newOtp);
$('.nd').html(orderDate);
$('.newaed').html(aedNew);
$('.newuser').html(order3);
$('.new-order-id').html(orderID);
$('.variations').html(variation);
$('#quantity').html(quantity);
},
complete: function() {
// Schedule the next request when the current one's complete
setInterval(sendRequest, 200); // The interval set to 5 seconds
}
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
