Search code examples
phpjqueryajaxcodeigniter-3

How do I display the amount without refresh?


I am using CodeIgniter. I have a function called as reloadCart(). I have to reload this function every time because this function will reload the amount anything changes happened. This is working perfect but the issue is when I am refreshing the page then it displays the new amount.

$(document).ready(function(){
reloadCart();// function reload on page refresh
    function reloadCart(){
    $.ajax({ 
            url: "<?php echo base_url(); ?>Member_controller/primaryCartload",
            context: document.body,
            success: function(data){
          if (data !=0) {
                var obj = JSON.parse(data);
               if (obj.qty != 0) {
               $('#totalDetails').html(obj.cart_total);          
                $('#totalQty').html(obj.totalQty);
               }
             }

          else{
              //alert('empty')
               $('#totalDetails').html('0');          
               $('#totalQty').html('0');
             }

            }

          });
    }
 });

Can we use something like?

load({
reloadCart();
});

Solution

  • You can just update your price by running that AJAX call time to time, for that you'll need to set a time after that specific time your call will be run again.

    var count = 20;
    setInterval(function () {
       count--;
       if (count === 0) {
          count = 20;
          reloadCart();
       }
    }, 1000);
    

    So after every 20 sec this function will be called, you can amend it as required.