Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:20:53+00:00 2026-05-15T18:20:53+00:00

Now I currently have an array that contains 120 pairs. For example: [[1, 12],

  • 0

Now I currently have an array that contains 120 pairs. For example:

[[1, 12],
 [2, 15],
 [3, 10]]

Essentially 120 long. Now I am using this data in a Jquery Flot graph. “I have a live example on jsFiddle here“

Code:

var numbers = [];
var jsonString = "";
var PlotData;
var x = 0;

function EveryOneSec() {
  if (numbers.length == 120) {
      numbers.shift();
  }
  numbers.push([x++, Math.random()*10]);
  PlotData = numbers;
  $.plot($("#PlaceHolder"), [{ data: PlotData, points: { show: true},lines: { show: true }}]);
    console.log(PlotData);
  setTimeout(EveryOneSec, 1000);
}
EveryOneSec();

I am creating the numbers on the fly (see jsFiddle Example) just with a simple increment of 1 for the first number and math random for the 2nd now this all works well and fine and sits their counting up again see the jsfiddle example.

Now what I would like to try and do is instead of increasing the numbers by 1 is play with the array. So that the first pair of the array is always [1, ##] and the 120th is always [120, ##] for all within the array. The idea being that it updates every 1 sec so the final result closest to the axis on the left is 120 showing 120 secs ago or 2mins.

So the question is: What would be the most efficient way to do this?

Because the number added on the fly is going to become outdated as soon as it does the next update.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:20:54+00:00Added an answer on May 15, 2026 at 6:20 pm

    An array with 120 element does not take long to iterate, so doing a for…next on it is ok, IMO. Cleaning up the code, you could have :

    var numbers = [];
    var jsonString = "";
    
    function EveryOneSec() {
      numbers.push(Math.round(Math.random()*10));  // generate integer from 0-10
      numbers = numbers.slice(-120); // keep only the last 120 max elements every time
      var PlotData = $.map(numbers, function(n,i) { return [[i+1,n]]; });
      $.plot($("#PlaceHolder"), [{ data: PlotData, points: { show: true},lines: { show: true }]);
      console.log(PlotData);
    }
    var timer = setInterval(EveryOneSec, 1000);
    
    // you can stop the timer with clearInterval(timer);
    

    ******EDIT**** : the code above will “scroll” the points from right to left, to reverse this, you can unshift the data instead of pushing, simply replace the first two lines of the function by this

    numbers.unshift(Math.round(Math.random()*10));  // generate integer from 0-10
    numbers = numbers.slice(0, 120); // keep only the fist 120 max elements every time
    

    you will “scroll” from left to right 🙂

    To reverse the axis (from 120 to 0), simply replace the returned value : [[120-i,n]]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a school assignment that involves both PHP and asp.net. Now the
We're currently running with php 5.2.5. We have now encountered a bug that creates
I'm currently doing something like this in some code I'm working on right now:
I am currently using NUnit and testing a class library. Up until now, I
I'm currently trying to make a small application that performs different duties. Right now
Now that I know C++ I want to get into desktop application that have
i`m currently playing around with WPF and now i wonder what would be the
I'm currently designing a database for a project. Now I'm debating with myself whether
I'm porting a library from Windows to *NIX (currently OSX), does anyone now what
Now that most of the major browsers support full page zoom (at present, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.