0

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

Jonáš Krutil
  • 245
  • 1
  • 9
  • Have you tried using a real controller name instead of ***Controller? Or are you just using that as an example? – theatlasroom Feb 01 '15 at 00:11
  • Just an example. But as I said. The problem is setTimeout function. Without setTimeout, script is working fine. It looks something like Laravel don't want to update model when I am not calling the route immediately after page load. – Jonáš Krutil Feb 01 '15 at 08:04

1 Answers1

0

I solved it.

I found out "delayed request content" is from cache.

enter image description here

Then I edit my code with help from "Problems with cached result when sending a XMLHttpRequest?".

xhr.open("GET", "{{ url() }}/increase/{{ id }}?t=" + Math.random(), true);

and my code is working even with

setTimeout( pageViewIncrease, 3000 );
Community
  • 1
  • 1
Jonáš Krutil
  • 245
  • 1
  • 9