Any reason why having a javascript timer fire every 2.5 second for an ajax request to my own callback(listed below) that all it does is return the contents of 2 _session variables would take up so much resources?
John
function refreshcarttimer() {
print($_SESSION['num_items'] . ',' . $_SESSION['uc_price']);
return;
}
/**
* Implementation of hook_menu().
*/
function product_type_menu() {
$items = array();
$items['cart/refreshcarttimer'] = array(
'title' => 'Refresh Shopping Cart',
'description' => 'Refresh Shopping Cart',
'page callback' => 'refreshcarttimer',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
I have a javascript timer set to fire every 2.5 seconds so that I can set the current values for the items and total for the shopping cart summary. I would like to be able to get these values from client side cookies or javascript global variables so that I don't have to make an ajax call to the server but I have found that the DOM elements are not accessible within the javascript function unless I run an ajax request to a page (listed below) and after it returns then the DOM elements are accessible (i.e. $(".num-items").html(numItems);) and now I can update them. Having these ajax requests run every 2.5 seconds for every client will not scale.
How can I make Drupal DOM elements accessible within a javascript function without having to do an ajax request to a page?
Thanks,
John
setInterval('updateShoppingCartInfo()', 2500);
function updateShoppingCartInfo(){
var xmlHttpReq = new XMLHttpRequest();
var url = 'https://' + document.domain + '/cart/refreshcarttimer';
xmlHttpReq.open('post', url, true);
xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState != 4) {
return;
}
else {
var responseText = xmlHttpReq.responseText;
var numItems = responseText.substr(0,responseText.indexOf(","));
var ucPrice = responseText.substr(responseText.indexOf(",")+1);
$(".num-items").html(numItems);
$(".uc-price").html(ucPrice);
}
}
xmlHttpReq.send(null);
}--
John J. Mitchell