Blocking Behaviour After Ajax Call To Slow Function.

Hi.

I have a problem which I cant seem to solve and I’ve tried lots of things.

I have a page where it shows "Online Status" to a user.

So if the app is online, it shows "Online".

Checking for internet connectivity status is not instant function, so I

decided to use AJAX to populate the div, so loading of this page does not

block. It’s working as expected, BUT while this “isOnline()” function

doing its thing, I cant go anywhere on my page. If I click on any other link,

it just sits there until "isOnline()" internet checking function completes

its run. To be exact, I’m doing the following:

In my view.php:




<script type="text/javascript">

/*<![CDATA[*/

jQuery(window).load(function() {

	jQuery.ajax({

	  url: '/overview/internetcheck',

    type: "POST",

    success: function() {

      jQuery('#internet_status').html('ONLINE');

    }

  });

});

/*]]>*/

</script>


  <div id="overview_block_1" style="width:485px;">

<?php

$dataProvider = new CArrayDataProvider(

    array(

        0 => array(

            'id'      => 0,

            "heading" => "Internet Status:",

            "value"   => '<div id="internet_status"><img src="/images/ajax-loader.gif"></div>',

        ),

    )

);


$this->widget('zii.widgets.grid.CGridView', array(

    'enablePagination' => false,

    'summaryText'      => "",

    'id'               => 'overview-block-1-grid',

    'dataProvider'     => $dataProvider,

    'template'         => "{items}",

    'columns'=>array(

        array(

          'name'        => 'heading',

          'header'      => Yii::app()->params['appName']. ' Info',

          'htmlOptions' => array('width'=>'35%', 'style'=>'text-align:right;padding-right:10px;'),

        ),

        array(

          'class'       => 'MyOverviewCDataColumn',

          'name'        => 'value',

          'header'      => "",

          'htmlOptions' => array('width'=>'65%', 'style'=>'text-align:left;padding-left:10px;'),

        ),

    ),

));

?>

  </div>



In my overview/controller file:




       public function isOnline($host = 'devatron.com', $port = 80, $timeout = 10) {

	  $errno  = 0;

	  $errstr = '';


	  if ($sock = @fsockopen($host, $port, $errno, $errstr, 10)) {

	    @fclose($sock);

	    return TRUE;

	  }

	  else {

	    return FALSE;

	  }

	}


	public function actionInternetcheck()

	{

	  if ($this->isOnline()) {

  	    header('Content-Type: application/json; charset="UTF-8"');

  	    echo CJSON::encode(array('result'=>'online'));

  	  }

  	  else {

  	    header('Content-Type: application/json; charset="UTF-8"');

  	    echo CJSON::encode(array('result'=>'offline'));

  	  }

  	  Yii::app()->end();

	}



This works as I want it (internet_status div’s spinner gets replaced with word “ONLINE”), except I cant click on any other link in my app until isOnline() completes.

Is there any way to stop this "blocking" behavior?

Tried everything I could, nothing works.

Please help.

Hi jlaguma,

Did you try $(document).ready() instead of $(window).load() ?

I’ve tried:

  1. jQuery(function() {…

  2. jQuery(document).ready(function() {…

  3. jQuery(window).load(function() {…

They all work at starting my internet checking action. Problem is that in all of the above cases that function blocks entire page. I cant click and go directly to another link. I have to wait for that function to end:


public function isOnline($host = 'devatron.com', $port = 80, $timeout = 10) {

          $errno  = 0;

          $errstr = '';


          if ($sock = @fsockopen($host, $port, $errno, $errstr, 10)) {

            @fclose($sock);

            return TRUE;

          }

          else {

            return FALSE;

          }

        }



Completely out of ideas on this one.

Hours later I have the solution.

I put session_write_close(); at the beginning on my isOnline() function.

That fixed it.

Dug out solution by googling for "how-to-prevent-blocking-php-requests".