1

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

5
  • Does this time must be updated in interval or one time display? Commented May 17, 2021 at 7:00
  • Why make an ajax request when you can get the same time() value in javascript with Math.floor(Date.now()/1000) ? Commented May 17, 2021 at 7:02
  • I need to display the time() for 3 different div's, when I visit the URL with 3 different Id's as http://www.project.test/getTime/id1 It should display the same/different time for each div's, So I need AJAX to get the request. @IGP Commented May 17, 2021 at 7:07
  • @Justinas, the time() should be different/same for the each div's, When I pass the URL as http://www.project.test/getTime/id1, It should display the time() for each div's respectively. Commented May 17, 2021 at 7:09
  • And when will you pass the url? After clicking on a button? Commented May 17, 2021 at 7:11

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.