difficulty encountered when passing parameters in functions such as setInterval

Hi everyone, I need some help with my code. Feel free to check it out here

Currently, I'm working on implementing multiple circular progress bars that can function dynamically. The goal is to be able to add additional progressCircle_# objects with different percentage values as needed. While the progress bars are loading the data and executing the animation, I am encountering an issue when inspecting the element in the browser. The error message "ReferenceError: start is not defined" keeps appearing. I would appreciate any suggestions on how to solve this problem. Thank you!

var progressCircle_1 = {
  procent: 89,
  startFrom: 0,
  incrementBy: 1,
  canvasId: 'canvas',
  procentId: 'procent',
  funct: function() {
    var start = setInterval(function() {
      draw.call(progressCircle_1)
    }, 50);
  }
}
var progressCircle_2 = {
  procent: 59,
  startFrom: 0,
  incrementBy: 1,
  canvasId: 'canvas1',
  procentId: 'procent1',
  funct: function() {
    var start = setInterval(function() {
      draw.call(progressCircle_2)
    }, 50);
  }
}

progressCircle_1.funct();
progressCircle_2.funct();



function draw() {
  (this.startFrom < this.procent) ? this.startFrom++: clearInterval(start);
  var getCanvas = document.getElementById(this.canvasId).getContext('2d');
  var getNumber = document.getElementById(this.procentId).innerHTML = this.incrementBy++;
  getCanvas.beginPath();
  getCanvas.arc(250, 250, 100, 0, 0.06283185307179587 * this.startFrom);
  getCanvas.lineWidth = '15';
  getCanvas.strokeStyle = "white";
  getCanvas.lineCap = "round";
  getCanvas.stroke();
};
#canvas {
  border: 1px solid red;
  transform: rotate(0deg);
}

#procent {
  font-size: 65px;
  color: white;
  position: absolute;
  top: 160px;
  left: 200px;
}

#procent::after {
  content: '%';
}

.container {
  background-color: lightblue;
  height: 500px;
  width: 500px;
}

#canvas1 {
  border: 1px solid red;
  transform: rotate(0deg);
}

#procent1 {
  font-size: 65px;
  color: black;
  position: absolute;
  top: 660px;
  left: 200px;
}

#procent1::after {
  content: '%';
}

.container1 {
  background-color: lightgrey;
  height: 500px;
  width: 500px;
}

#canvasProgressBar {
  position: relative;
  top: 100px;
  left: 10px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <meta name="description" content="Interaktywny poradnik szybkiego startu dla Brackets.">
  <link rel="stylesheet" href="main.css">
</head>

<body>
  <div class="container">
    <canvas id="canvas" width="500" height="500">
</div>
<p id="procent"></p>
<div class="container1">
    <canvas id="canvas1" width="500" height="500">
</div>
<p id="procent1"></p>

  <script src="main.js"></script>
</body>
</html>

Answer №1

To improve the code, place var start at the beginning of the script instead of within the funct function

Updated JavaScript Code:

var start

var progressCircle_1 = {
  procent:89,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas',
  procentId:'procent',
  funct: function(){
    start = setInterval(function(){draw.call(progressCircle_1)},50);
  }
}
var progressCircle_2 = {
  procent:59,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas1',
  procentId:'procent1',
  funct: function(){
    start = setInterval(function(){draw.call(progressCircle_2)},50);
  }
}

progressCircle_1.funct();
progressCircle_2.funct();


function draw(){
  (this.startFrom<this.procent)?this.startFrom++:clearInterval(start);
  var getCanvas = document.getElementById(this.canvasId).getContext('2d');
  var getNumber = document.getElementById(this.procentId).innerHTML=this.incrementBy++;
  getCanvas.beginPath();
  getCanvas.arc(250,250,100,0,0.06283185307179587*this.startFrom);
  getCanvas.lineWidth='15';
  getCanvas.strokeStyle="white";
  getCanvas.lineCap="round";
  getCanvas.stroke();
};

Answer №2

Make sure to also store the interval id: https://jsfiddle.net/x8Lypm2j/

var progressCircle_1 = {
  procent:89,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas',
  procentId:'procent',
  intervalId: 0,
  funct: function(){
    this.intervalId = setInterval(function(){draw.call(progressCircle_1)},50);
  }
}
var progressCircle_2 = {
  procent:59,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas1',
  procentId:'procent1',
  intervalId: 0,
  funct: function(){
    this.intervalId  = setInterval(function(){draw.call(progressCircle_2)},50);
  }
}

progressCircle_1.funct();
progressCircle_2.funct();


function draw(){
  (this.startFrom<this.procent)?this.startFrom++:clearInterval(this.intervalId);
  var getCanvas = document.getElementById(this.canvasId).getContext('2d');
  var getNumber = document.getElementById(this.procentId).innerHTML=this.incrementBy++;
  getCanvas.beginPath();
  getCanvas.arc(250,250,100,0,0.06283185307179587*this.startFrom);
  getCanvas.lineWidth='15';
  getCanvas.strokeStyle="white";
  getCanvas.lineCap="round";
  getCanvas.stroke();
};

Answer №3

When passing start to the calling function, you specify that the function's this is progressCircle_1. However, since progressCircle_1 doesn't have a start attribute, you need to pass the timeoutId(start) to the calling function.

    var progressCircle_1 = {
  procent:89,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas',
  procentId:'procent',
  funct: function(){
    var start = setInterval(function(){draw.call(progressCircle_1,start)},50);
  }
}
var progressCircle_2 = {
  procent:59,
  startFrom:0,
  incrementBy:1,
  canvasId:'canvas1',
  procentId:'procent1',
  funct: function(){
    var start = setInterval(function(){draw.call(progressCircle_2,start)},50);

  }
}
progressCircle_1.funct();
progressCircle_2.funct();


function draw(start){
  (this.startFrom<this.procent)?this.startFrom++:clearInterval(start);
  var getCanvas = document.getElementById(this.canvasId).getContext('2d');
  var getNumber = document.getElementById(this.procentId).innerHTML=this.incrementBy++;
  getCanvas.beginPath();
  getCanvas.arc(250,250,100,0,0.06283185307179587*this.startFrom);
  getCanvas.lineWidth='15';
  getCanvas.strokeStyle="white";
  getCanvas.lineCap="round";
  getCanvas.stroke();
};

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

Adjust the size of the Threejs canvas to fit the container dimensions

Is there a way to determine the canvas size based on its container in order to prevent scrolling? Setting the size based on the window results in the canvas being too large. ...

Arrangement of Divs in Mobile Design - Utilizing Bootstrap 4 Rows and Columns

I've been grappling with a layout issue while using Bootstrap 4, specifically concerning the positioning of elements on mobile devices. I was wondering if anyone could lend me a hand with this challenge. Let me illustrate the desired layout: Link to ...

The functionality of the "Slots" prop has no impact when used on the material-ui Slider component

Trying to understand the purpose of the "slots" prop in relation to customizing how inner components like track and thumb are rendered within the Slider component. A basic example of rendering a Slider component is shown below const marks = [ { value: 0 ...

Ajax request in Rails not receiving a response from the controller

After troubleshooting a simple GET request to the controller action, I confirmed that the request is being made successfully and there are no issues with the controller executing the action. However, I am not receiving any response data. $.ajax({ url: ...

What distinguishes the installation of eslint as an extension from installing it as an npm package?

After researching multiple blogs and videos about the setup and configuration of eslint and prettier for vscode development, I've noticed a common gap in explanation. None of the resources adequately clarify why it's necessary to install eslint a ...

"Transforming JSON data into a format compatible with Highcharts in PHP: A step-by-step

Currently facing an issue with converting the given array format into a Highcharts compatible JSON to create a line chart. Although everything else is functioning correctly, I am struggling with this specific conversion task. { name: [ 1000, ...

Maintaining consistent row height in bootstrap when displaying or hiding a button

I'm currently working on a project utilizing AngularJS and Bootstrap to create a dynamic form. My goal is to have an 'edit' button display when a user hovers over a specific row of the form. However, I'm facing an issue where the row&ap ...

When attempting to add mp3 files to a Vue/TypeScript application, a "Module not found" error is triggered

I am encountering an error while trying to import .mp3 files into my Vue/Typescript app. Upon running 'npm serve', I am presented with the following message: ERROR in /Users/***/***/application/src/components/Sampler.vue(80,16): 80:16 Cannot fin ...

Having issues retrieving a JSON array in PHP with the json_decode function

Can someone assist me with passing and returning an array to a PHP script? I have successfully tested the json_encode portion, but I am facing issues with the json_decode on the PHP side. Javascript scid_list = []; $('.filter_on').each ...

Toggle class to a div upon clicking menu item

Seeking assistance with jQuery to develop a video player featuring a sub menu for displaying various content options upon selection. Here is a snapshot of the frontend design: view image Upon clicking on 'video' or 'audio', a distinct ...

Attempting to trigger an action from a Vuex module proves futile as the error message "[vuex] unknown action type" is generated

I've been struggling to successfully register a Vuex module. Below is the code I've been working with: stores/index.js: import Vuex from 'vuex'; import resourcesModule from './resources'; import axios from '@/helpers/ax ...

Navigating the Drift

I am a beginner in HTML/CSS and need some assistance. Here is the layout I am working with: <style> #main {background-color: red; width: 30%;} #right_al{float: right;} #to_scroll{overflow: scroll;} </style> <div id='main'> ...

Is it possible to combine Django urls and Vue routes in a single project?

After setting up my Django app and implementing the authentication layer using Django-Allauth with features like email confirmation, password reset, and two-factor authentication, I've come to the realization that for my app's interactive nature ...

I'm encountering a type error every time I attempt to render an EJS page. Could someone please take a look and help me troubleshoot?

Below is the index.js code: CODE HERE const express = require('express'); const app = express(); const path = require('path'); app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded app.use(ex ...

Python regular expression problem with matching regex

Hey there, I'm diving into my first question on stackoverflow and I've been struggling with it for hours. I'm sure the solution is right in front of me, but I just can't seem to find it. My goal is to extract information from a webpage ...

How to send an email using asp.net and html

This is the form on my index.aspx page. <form role="form" method="post" action="SendMail.aspx"> <div class="form-group"> <input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div> ...

Enhance Laravel 5 by integrating browserify into the Elixir build process

My workflow for transforming coffee to js using browserify, browserify-shim, and coffeeify looks like this: I work with two main files: app.coffee and _app.coffee, designated for frontend and backend respectively. These files are located in resources/coff ...

Developing an npm module that is compatible with both web browsers and Node.js

Currently, I am in the process of developing an npm package that will cater to both web apps and other node modules. If my focus was solely on browsers, I would simply assign window.myExport = myExport; as a direct solution (unless there is a more contemp ...

How can I display a calendar with a complete month view using ng-repeat?

I was trying to replicate a table similar to the one shown in this image: (disregard the styling). I am struggling with how to properly format the data to create a similar table in HTML. $scope.toddlers = [ { "name": "a", "day": 1, "total": 3 }, { ...

Integrating Vimeo videos into Angular applications

I am attempting to stream videos using URLs in my Angular application. Every time I try, I encounter the following error: Access to XMLHttpRequest at 'https://player.vimeo.com/video/548582212?badge=0&amp;autopause=0&amp;player_id=0&amp;ap ...