CodeIgniter Forums
Auto Refresh a function in controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Auto Refresh a function in controller (/thread-53822.html)



Auto Refresh a function in controller - El Forum - 08-10-2012

[eluser]hema[/eluser]
Hi All,

I am having two different sites with single database. In site1, people will start commenting on one article written by author. Now in the site 2 that author should get automatic update on the comment without refreshing his page. And refresh time should not be more time, it will be better if update happens as soon as people comments are done.

Can anyone suggest me how to achieve this either with ajax or any plugin already available for updating controller every second without refreshing page.





Auto Refresh a function in controller - El Forum - 08-10-2012

[eluser]LuckyFella73[/eluser]
This is a code snipped I used to update my view:

Code:
var interval;

function Check_session() {
  
  $.ajax({
   url: ci_base_url + "controller_name/method_name",
   dataType: "json",
   success: function(data) {
    if (data.new_record == true) {
    
     closeInterval();
   // view update logic - display message - new post
    }
   }
  });
};

function startInterval(sec){
  
  interval = setInterval(function(){

   Check_session();

  }, sec);
};

function closeInterval(){
  clearInterval(interval);
};

startInterval(10000); // set your own value here

The var "ci_base_url" can be set hardcoded - I echo the value
using base_url() in my page header:

Code:
<scr +++ ipt type="text/java +++ script">
var ci_base_url = '&lt;?php echo base_url();?&gt;';
</scr +++ ipt>

I assume you load jquery to do the ajax request ..

Hope that get you on the right track.


Auto Refresh a function in controller - El Forum - 08-10-2012

[eluser]hema[/eluser]
thanks for ur reply, but will this work(if (data.new_record == true) ) only for new records or any data updated in the table.

In the following way i am doing but did not get any auto update. This code is in my views


(loading jquery.js here)
Code:
<sc88>

function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
  xmlhttp=new XMLHttpRequest();
}
catch(e) {  
  try{  
   xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e){
   try{
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch(e1){
    xmlhttp=false;
   }}}
return xmlhttp;
}

</ ssss >


<s cc rr >

function Check_session() {



  $.ajax({

   url: "http://localhost/blog/index.php/post/postslist",

   dataType: "json",
   success: function(data) {
    if (data.new_record == true) {
    
     closeInterval();
   // view update logic - display message - new post
    }
     });
};

function startInterval(sec){
  
  interval = setInterval(function(){

   Check_session();

  }, sec);
};

function closeInterval(){
  clearInterval(interval);
};

startInterval(3000);
< / scc >



Auto Refresh a function in controller - El Forum - 08-10-2012

[eluser]InsiteFX[/eluser]
If you want people to read your posted code wrap in code tags.



Auto Refresh a function in controller - El Forum - 08-10-2012

[eluser]hema[/eluser]
thanks 4 suggestion, after a very long(almost 1 year) i am using this forum. So i did not notice it. I updated my code with code tags