call an action each second

Hello, I want to call an action each seconds.

My controller is:




class AlarmaController extends Controller

{

......


public function actionValidarTimeOutSMS(){

            

            $val1 = $_POST['val1'];

            $val2 = $_POST['val2'];

            echo "something.. HELLO";


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

        }

.........

}




and my view is:


<script type="text/javascript">

          $(document).ready(function(){

                    setInterval(alertFunc, 3000);

            });

            function alertFunc() {

                $.ajax({

                    type: "POST",

                    url:    "<?php echo Yii::app()->createUrl('alarma/ValidarTimeOutSMS'); ?>",

                    data:  {val1:1,val2:2},

                    success: function(msg){

                         alert("Sucess")

                        },

                    error: function(xhr){

                    alert("failure"+xhr.readyState+this.url)


                    }

                  });

                

            }

    

      </script>

it never show me something… HELLO

Thanks you for your help, I rally I need that it work…

You may have not loaded the jQuery. Your code is working fiddle at https://jsfiddle.net/s12qbfke/1/

I tryed it but It’s not work. It show me the erros message: failure4<?php echo Yii::app()->createUrl(‘alarma/ValidarTimeOutSMS’);?>

If you check it using the network you will see the ajax request sent. You are getting the error because the url is not there. So 404 error occurs.