Reload only one DIV with Javascript

Asked

Viewed 103 times

-2

Hello. I am using LARAVEL.

I have a div that displays whether the site is ON or OFF. Using the curl_info function, the url is passed and then the function returns the site status.

I would like this div to be constantly updated to check when the site gets OFF. How to update only this div and not the whole site with Javascript?

PHP

public function curl_info($url){
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HEADER, 1);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );

        $content = curl_exec( $ch );
        $info = curl_getinfo( $ch );

        if ($info['http_code']==200 ) {
                return "ON";
                } else {
                return "OFF";
                }

    }
<div class="col-3 text-center font-weight-bold border border-light {!! app(App\Http\Controllers\monitController::class)->curl_info('http://google.com') !!}" id="meupc">
            SITE

            <p>{!! app(App\Http\Controllers\monitController::class)->curl_info('http://google.com') !!}</p>
          </div>
  • 1

    setInterval + AJAX

  • What is your question? How to run the method from time to time, how to update the div or how to do Curl?

  • Just to confirm, you change the div so you know? $('#suaDiv'). text('content') OR in JS: Document.getElementById("bla"). innerHTML = "new content of div";

1 answer

0

Try incrementing this in your code, only instead of leaving the execution of function myFunction() in a button, leaves in the onload of body.

function myFunction() {
setInterval(function(){
  console.log("executando")
},1000)
}
<button onclick="myFunction()">Try it</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.