0

Apologies if this particular question has been solved before, I have looked everywhere it seems and can't quite get the answer I'm looking for! I am no expert and can imagine the solution is embarrassingly easy.

My problem is this: I have some php and javascript code working on a html based website, linked to a database (reading data in and also writing data out via a save function called once at the end of the script). I need the javascript code to automatically save/update itself to db via an Ajax request, without the need to keep running the page. The data being saved here needs to be read by various other pages and is relied upon to give correct results elsewhere! (so a solution would be to have the user keep the page open in the background - but suggestions for this separate issue are also welcome!)

Anyway, at the moment I have:

 function sessionSave () {
    var newData = kpiCA.getData().concat(kpiHA.getData(),kpiStocks.getData(),kpiCV.getData(),kpiPD.getData());
    $.ajax({
      url: 'saveMain.php',
      type: 'POST',
      data: {'kpi': newData},
      success: function () {

      },
      error: function () {
        $console.text('Data Save Error');
      }
    });
  }
  sessionSave();

I have seen some autosave scripts and the addition of timers etc. but as I am a complete noob, some help would be much appreciated,

Thanks guys!

Laura Thomas
  • 653
  • 1
  • 5
  • 5
  • "_without the need to keep running the page_" Not quite sure I get what you're asking. Should the website automatically update things even when no visitor/user is there? If so, javascript seem quite pointless - since it only runs on client. But please clarify your request. – Mackan May 14 '15 at 14:36
  • 1
    So you want to run `sessionSave()` every x minutes? Look at this topic http://stackoverflow.com/questions/2170923/whats-the-easiest-way-to-call-a-function-every-5-seconds-in-jquery – breq May 14 '15 at 14:38
  • Hi thanks for your comment, sorry it was a bit unclear! The running the page refers to the need for a user to keep opening/reloading the page for the save to happen. It does not need to update if the user is not there, it can run in the background while the user has the said page open. – Laura Thomas May 14 '15 at 14:39
  • 1
    Then using setInterval and saving the data as @breq mentioned is the way to go. – Michael Todd May 14 '15 at 14:42
  • Perfect @breq :D Seems I was over complicating things. – Laura Thomas May 14 '15 at 14:42
  • Thanks @Michael. Just to pick your brains as you mentioned the need for the use to be there or not and this is something that would definitely improve my program - how could this be done? Client side via? – Laura Thomas May 14 '15 at 14:44
  • "The user to be there" is throwing me off; I'm not sure what you mean by that. If a web page is open in a browser tab, then the setInterval function will run whether or not the user is actively viewing the tab. Also, there is no need for them to refresh the page as the setInterval will work even if they do not refresh it. – Michael Todd May 14 '15 at 14:49
  • By user being there, I mean that they do have to have the page open regardless if they view it etc. Is there a way to improve this so that a user does not have to have the page open at all? – Laura Thomas May 14 '15 at 14:52
  • 1
    Yes, the page has to be "open" (either on a viewed or unviewed tab) for the Javascript to run. If the web page isn't open, the Javascript can't run. Anything that needs to be done when the user is not viewing the page (i.e. client-side) can be done server-side, as inferred by @Mackan. – Michael Todd May 14 '15 at 15:03
  • Just realised I've confused you with @Mackan - apologies guys. But thanks to all, it's much clearer now, I will look further into the server side saving solution :) – Laura Thomas May 14 '15 at 15:06

1 Answers1

0

Basically it's just timers or intervals. For example:

window.setInterval(sessionSave, NUMBER_OF_SECONDS * 1000)
// where NUMBER_OF_SECONDS is, obviously, the number of seconds to repeat your function at
Marius
  • 3,976
  • 5
  • 37
  • 52