For a few hours I am stuck on this problem.
I have a url
increase/1
and route
Route::get('increase/{id}', '***Controller@action***');
and controller
public function action***($id) {
$model = ***::where***->first();
$model->counter += 1;
$model->save();
}
When I hit url in browser, counter is incremented -> DB updated.
When my template is rendered and I want to call this url through XMLHttpRequest. I have a script on the bottom of page (xhr.timeout is not important).
<script>
function pageViewIncrease() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "{{ url() }}/increase/{{ id }}", true);
xhr.timeout = 0;
xhr.send(null);
}
pageViewIncrease();
</script>
When template is rendered, counter is incremented -> DB updated.
But I want to call this "script" with delay.
setTimeout( pageViewIncrease, 290 );
With timeout, counter is sometimes incremented and sometimes not.
With bigger timeout (500,1000,...), Laravel not update model anymore.
I really don't know what am I doing wrong :(.
Thank you