My code includes a function that uses setInterval to generate graphs

Is there a way to stop the setInterval function without causing the graph to disappear? I've noticed that when the setInterval stops, the last 7 points plotted on the graph disappear. How can I prevent this from happening? Any suggestions would be greatly appreciated. Thank you!

`

var graphingID = setInterval(function(){
  if (itertn == char.length){
    clearInterval(graphingID);
   }
  itertn++;
   //Add two random numbers for each dataset
  myLiveChart.addData([char[itertn], char[itertn]], ++latestLabel);
  // Remove the first point so we dont just add values forever 
  //updateDaw();
  myLiveChart.removeData(2);
}, 50);

`

Answer №1

Maybe we can try something similar to this code snippet?

let intervalID = setInterval(function(){
  if (counter == characters.length){// Should we stop now? If yes, clear the interval
     clearInterval(intervalID);
   }
  else{// If not, keep going

      counter++;
       // Add two random numbers for each dataset
      myChart.addData([characters[counter], characters[counter]], ++latestLabel);
      // Remove the first point to avoid adding values indefinitely
      //updateView();
      myChart.removeData(2);
 }
}, 50);

Answer №2

Avoid proceeding after clearing:

if (itertn == char.length){ clearInterval(graphingID); return; }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to Iterate Through an AJAX Response

I'm working on a tagger application. When a user submits a tag, ajax sends the following response: {"returnmessage":"The Ajax operation was successful.","tagsinserted":"BLAH, BLOOOW","returncode":"0"} My goal is to extract the tags inserted and dyna ...

Determine the present vertical measurement of the video

I am having trouble determining the actual height of a video element. Despite seeing in the developer tools that it is 384px, I am only able to retrieve a height of 342px. I attempted the following code snippet, but it only gives me the natural height, no ...

Utilizing Vue.js for Tabs in Laravel 5.8

I am encountering an issue in Laravel while attempting to set up a Vue instance for tabs. Currently, only Tab 1 and Tab 2 are displayed without any content, and the tabs themselves are not clickable links. Could this problem be related to how I am calling ...

Setting a timeout from the frontend in the AWS apigClient can be accomplished by adjusting the

I am currently integrating the Amazon API Client Gateway into my project and I have successfully set up all the necessary requests and responses. Now, I am trying to implement a timeout feature by adding the following code snippet: apigClient.me ...

The Mantine date picker is causing an error stating that objects are not valid as a React child

I'm currently experimenting with utilizing Mantine Date in NextJS. The goal is to have the selected date displayed in the HTML text heading when a user clicks on it. For instance, if a user selects January 1st, 2023, the text should show like this: Da ...

mdcolors not displaying correctly within mddialog component in Angular Material

In my mddialog, I attempted to use md-colors to change the background color of a div within the dialog. Unfortunately, it is not adhering to the current theme and instead defaults to the blue theme. md-colors="{backgroundColor: 'primary'}" Has ...

AngularJS ui-router, html5Mode, and Express causing "Cannot GET" error in nested state

Currently, I am facing an issue while trying to implement $locationProvider.html5Mode(true) with AngularJS, ui-router, and ExpressJS. The problem arises when navigating to a nested state parent.child with URL /child, as refreshing the page or directly typi ...

Click on the link to go to another page according to the drop-down option selected

Here's a puzzling query that even Google fails to address. I've got two pages: page-1 and page-2. On page-2, there is a <ul> element and a toggle-button-box with the following code: <ul> <li><button class="button is-checked ...

AngularJS datetimepicker integrated in the HTML script tag

<div class='input-group date' id='datetimepicker'> <input type='text' class="form-control" ng-model="ctrl.startDate" /> <span class="input-group-addon"> <span class="fa fa-calendar"></span> ...

Passing arguments from an Angular directive to a controller function within an element's HTML

Is there a way to pass the URL of an anchor tag in the directive to the controller function "itemClicked"? The code below successfully logs the "post" object when clicked. HTML: <div ng-repeat="post in posts" > <div find-urls link-clicked="i ...

Substitute all instances of null bytes

I need to remove null bytes from a string. However, after replacing the null bytes \u0000 in the string let data = {"tet":HelloWorld.\u0000\u0000\u0000\u0000"} let test = JSON.parse(data).tet.replace("\u0000", ""); I always ...

Only display PHP page content if accessed through an AJAX request, not through directly typing the URL into the address bar

Is there a way to verify if a PHP page has been accessed via an Ajax request? I'm working on a page that displays a form for updating some database data (update.php), but I only want it to be accessible if it is called by a specific page named "change ...

Enhance the functionality of the 'validate as true' function

I have an object that resembles the following $scope.object = { Title: 'A title', Status: 'Open', Responsible: 'John Doe', Author: 'Jane Doe', Description: 'lorem ipsum dolor sit' } My aim now i ...

Is there a way to utilize a JavaScript function to transfer several chosen values from a select listbox to a different listbox when a button is clicked?

Is it possible to create a functionality where users can select multiple values from a first list and by clicking a button, those selected values are added to a second list? How can this be achieved through JavaScript? function Add() { //function here ...

Sending an Ajax request to C# code

UPDATE After some research, I discovered what seems to be the "correct way" to handle the issue by using a combination of JSON.stringify and creating a model, as explained in this thread. I still can't figure out why the original approach didn't ...

JQGrid will automatically conceal any row that contains a false value in a given cell

I'm attempting to conceal a row if a specific cell within it contains the value false. To achieve this, I have experimented with using a formatter in the following manner: $("#list").jqGrid({ //datatype: 'clientSide', ...

The size of the Webpack bundle grows with each subsequent build

For my project, I am utilizing webpack to package it as a library. The project consists of a components library, and for each component residing in its own directory under src/ui, I am creating small bundles. Here is an example component structure: src/ ...

Switching from PHP to JavaScript before returning to PHP to establish and manage sessions

Currently, I am in the process of resolving an issue I am facing. The problem arises when utilizing the following code for someone trying to sign into the admin panel: <script> function myFunction() { //alert('you can type now, end ...

What is the best way to leverage a webbrowser control for design purposes while keeping the functionality in C#?

Observing some apps, it seems they utilize HTML/CSS/Javascript for styling - a much simpler approach compared to crafting the same thing natively. However, these apps have their logic written in C#. Sadly, I am clueless on connecting the two. Research yiel ...

Oops! Vue.js router configuration is throwing an error because it's unable to read properties of undefined when trying to access 'use'

Description: I have been working on a leaderboard web application using Vue.js. When I tried to launch the server on localhost after finishing my code, I encountered an error. The error message I received is as follows: Uncaught runtime errors: ERROR Cann ...