title: How do I get the last x elements in an array and save them in JavaScript (No JS experience)
body: <p><strong>WHAT am I trying to accomplish in JavaScript:</strong></p>
  <ol>
  <li>I am trying to save a constant screen recording as an array with every second being 1 element of the array</li>
  <li>I am trying to make the max length of the array 10, that being 10 seconds. So the Javascript has to constantly overwrite the array as it continues to record.</li>
  <li>when I stop the screen recording, I need to be able to save that 10 element array as a video that I can save on my machine.</li>
  </ol>
  <p><strong>WHY I am trying to accomplish this in JavaScript</strong></p>
  <ol>
  <li>I want to mimic NVIDIA shadowplay using javascript.</li>
  <li>I dont want to use up a bunch of hardware resources by having a crazy long video recording and then save only the last ten seconds</li>
  </ol>
  <p>Below is the important piece of code. What this does currently, is return the entire length of the array as long as its over 10 seconds I don't want this I only need the last 10 seconds so if its 20 seconds it returns 20.
  It also returns every second before the very last 1 second as a jumbled mess of pixels that do not move. I don't want this either as I need the last 10 seconds to be visual and if its saving 20 seconds of of the clip and returning it that means if it recording is 8 hours long it will use up a bunch of hardware resources and that just cannot happen.</p>
  <p>I've tried using the functions below that are commented out. slice, pop, and shift all corrupted the file and returned nothing, and if I'm not EXTREMELY careful with how I use the splice function that also returns a corrupted file.  This is the closest i've gotten to the end goal of what I'm trying to do, and it is only the primary faucet of the code that I need to write.</p>
  <pre><code>function createRecorder (stream, mimeType) {
    // the stream data is stored in this array
    let recordedChunks = [];
    array_length = 10;
    const mediaRecorder = new MediaRecorder(stream);
  
    mediaRecorder.ondataavailable = function (e) {
      if (e.data.size &gt; 0) {
        recordedChunks.push(e.data);
      }
      if (recordedChunks.length &gt; array_length)
      {
        recordedChunks.splice(-10, array_length);
        //const testarray = ['a', 'b', 'c', 'd'];
        //alert(testarray); // splice(-3,1) b *** slice(-3,1) __ *** slice(-3) b,c,d COPIED shallow array
        //alert(nonrecordedChunks); // splice(-3,1) a,c,d *** slice(-3,1) a,b,c,d *** slice(-3) a,b,c,d
      }
    };
    mediaRecorder.onstop = function () {
       //saveFile(recordedChunks);
       saveFile(recordedChunks);
       recordedChunks = [];
    };
    mediaRecorder.start(1000);
    return mediaRecorder;
  }
  </code></pre>
  <p>What am I doing wrong here? thank you for the help. I have no prior experience in JavaScript and at the time of writing this post I've put in 6 hours to get this far and am ready to headdesk.exe</p>