Yii2 Ajax xmlhttp.open

How i can use this function in my Yii2 aplication?

<script type="text/javascript">
        function ajaxrunning()
        {
            if (window.XMLHttpRequest)
            {
                xmlhttp=new XMLHttpRequest();
            }
            else
            {
                xmlhttp =new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.open("GET","autoload.php");
            xmlhttp.send();
            setTimeout("ajaxrunning()", 5000); 
        }
</script>


And i want to change the autoload.php to be the controller function http://localhost/ta/backend/web/index.php?r=sms/auto

You need to create the function in the controller to you make the call .

The path will be like this controllerName/actionName

Read this example

      //PHP UserController.php

        function actionSomething(){
          //do something
          return 'aaaa';
        } 

  //JS code 

<script type="text/javascript">
function ajaxrunning() {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);// this write 'aaaa'.
            }
            else if (xmlhttp.status == 400) {
                alert('There was an error 400');
            }
            else {
                alert('something else other than 200 was returned');
            }
        }
    }
    xmlhttp.open("GET", "/user/something");
    xmlhttp.send();
    setTimeout("ajaxrunning()", 5000);
}
</script>

I recomend to use JQuery and not Javascript Vanilla, is more easy and clear.