Reputation: 31
In my blade file, I have 3 div's, with 3 different ID's.
<div id ="id1">0</div>
<div id ="id2">0</div>
<div id ="id3">0</div>
The 0
in each div could be replaced by Time()
For ex, which gives the result as 1666111
.
If the Routes/web.php looks like this
Route::get('/getTime/{id}', 'App\Http\Controllers\TimeController@getTime');
How to use JQuery Ajax to fetch the time()
in the controller and to bind the value to each div's.
Could some one please help? Thanks
Upvotes: 1
Views: 44
Reputation: 15786
You don't need to pass the ids to the route. time()
expects no parameters.
Route::get('/getTime', 'App\Http\Controllers\TimeController@getTime');
public function getTime()
{
return time();
}
$(document).ready(function() {
$.ajax({
url: '/getTime'
method: 'get'
})
.done(time => {
$('#id1').text(time);
$('#id2').text(time);
$('#id3').text(time);
});
});
But this is a really convoluted way to get the unix timestamp on the divs. You could just do it like this.
$(document).ready(function() {
const unixTimestamp = Math.floor(Date.now()/1000);
$('#id1').text(unixTimestamp);
$('#id2').text(unixTimestamp);
$('#id3').text(unixTimestamp);
});
Upvotes: 1