0

Actually i am sending ajax requests with in a interval of 4-5 seconds to update a digital display table.

  • Should i use ajax GET or POST
  • What is ajax cache:true/false
  • If i populate DOM elements(some are marque) after each ajax call(in 4 seconds) then will it affect performance

My code is something like :

<table>
<?php for(i=0; $i<=10; $i++) { ?>
<tr>
    <td><span id="span-<?php echo $i;?>-1></span></td>
    <td><span id="span-<?php echo $i;?>-2>></span></td>
    <td><span id="span-<?php echo $i;?>-2>></span></td>
    <td><marque id="td-<?php echo $i;?>-4>></marque></td>
</tr>
<?php } ?>

<script>
$(document).ready(function() {
    autoRefresh_div();

    function autoRefresh_div()
    {
            dataString = 'x=fetchdata'; 
            $.ajax({
                type: "POST",
                url: "display_ajax.php",
                data:dataString,
                dataType: 'json',
                success: function(result){
                    $.each(result, function( index, value ) {
                        if (value['status']==1) {
                            $('#span-'+index+'-1').html(value['status']);
                            $('#span'+index+'-3').html(value['name']);
                            $('#span-'+index+'-4').html(value['type']);
                            $('#td-'+index+'-5').html(value['des']);
                        } else {
                            $('#span-'+index+'-2').html('N/A');
                        }
                    });
                },
                complete: function() {
                    setTimeout(autoRefresh_div, 4000);
                }
            });
        }
     });
</script>

And array returned as json from server is like

array
(
0 => array('status'=>2),
1 => array('status'=>1,'name'=>'xyz','type'=>'t1','des'=>'jfhj.....'),
2 => array('status'=>1,'name'=>'abc','type'=>'t2','des'=>'llll.....'),
3 => array('status'=>1,'name'=>'def','type'=>'t1','des'=>'ffhj.....'),
4 => array('status'=>2),
5 => array('status'=>1,'name'=>'jkl','type'=>'t4','des'=>'llol.....'),
6 => array('status'=>2),
7 => array('status'=>1,'name'=>'aaa','type'=>'t3','des'=>'lojl.....'),
8 => array('status'=>1,'name'=>'bbb','type'=>'t4','des'=>'loll.....'),
9 => array('status'=>1,'name'=>'lll','type'=>'t1','des'=>'lhol.....'),
)
Deepak Kumar
  • 129
  • 2
  • 13
  • "If i populate DOM elements(some are marque) after each ajax call(in 4 seconds) then will it affect performance" — Doing *anything* affects performance. Try it if you want to see how much effect it has and if you care about that. – Quentin Jan 04 '18 at 16:42
  • LMGTFY: `jQuery ajax cache` = If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET. – timothyclifford Jan 04 '18 at 16:43
  • What is the benefit of ajax cache – Deepak Kumar Jan 05 '18 at 15:52

0 Answers0