How to use setInterval trigger controller action?

Hi all,

I have facing a problem on loading products. I have a lot of product ( might be 500 above ) in the list, when i open the management page, i need to load all product in once. But the problem is the product is a lot and it will sometime cause timeout error and i unable to load all product or get empty result. So i would like to use setInterval to divide few time load the products. So every 5 second will trigger controller to continue load the products until finish. Can anyone teach me how to write the code or have any sample code to reference?

Hi,

in general you need to do something like this:

js code




<script>

$(document).ready(function(){

    var productsCount=<?php echo $productsCount; ?>;

    var it=1;

    var iid=setInterval(function(){

        $.ajax({

            'url':'products/list'

            'data': {'limit': 20, 'offset': 20*it},

            'success': function(response){

                $('#products_container').append(response);

                if(it*20>=productsCount){

                    clearInterval(iid);

                }

            }

        });

        it++;

    }, 5000);

});

</script>



in your products controller:




public function actionList()

{

    $criteria=new CDbCriteria(array(

        'limit'=>$_GET['limit'],

        'offset'=>$_GET['offset'],

    ));

    $models=Products::model()->findAll($criteria);


    $this->renderPartial('list', array('products'=>$models));

}



For js you can use something like this ‘lazy loading’ jQuery plugin

Why don’t you want to use pager mechanism, like this built into CGridView?

Hi drylko,

Thank a lot, this coding is exactly what i need. Hmm…i know it suppose to use paging cause more clean and easy but this is the client request., they want it to load once and can edit it in the same page without refreshing.