简体   繁体   中英

Auto saving JAVASCRIPT data via AJAX to database

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM