There is a syntax error in the for-loop within the AngularJS $http.get causing an unexpected identifier

I'm encountering a 'syntax error: unexpected identifier' and my browser seems to be getting stuck in a loop after executing this code. I figured incorporating setInterval for delaying API requests was a sound strategy according to the API request guidelines.

var tempArray = []; 
var arr = [];   
//angular controller    
for (var i = 0; i < 10; i++) {
    setInterval($http.get(url).success(function(data) {
        tempArray.push(data);
        arr.push(tempArray);
    }), 1000);
}

Answer №1

After @vlaz's explanation, it is clear that using setTimeout is preferable when you need to delay an action, while setInterval continuously calls the specified callback function.

I find it puzzling that the API guidelines suggest delaying requests... are they actually referring to "debouncing" instead?

In any case, encountering the "unexpected identifier" syntax error implies that a non-function argument was provided as the first parameter in setInterval. The correct approach is:

setTimeout(function() {
  $http.get(url).success(function(data) {
    tempArray.push(data);
    arr.push(tempArray);
  });
}, 1000);

The reason for this is that $http.get(url).success() does not return a callable function that can be passed directly to setTimeout.

Answer №2

Make sure that the $http is included in your Controller like this:

myApp.controller('MainController', function MainController($scope,$http) {...}

The use of setInterval allows the code to run repeatedly. In this scenario, it will loop every second.

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

At what point does the chaining of async/await come to an end?

I was experimenting with node-fetch and encountered a question while using async / await: Do I need to make my function async if I use await in it? But then, since my function is async, I need to await it and make the parent function async. And so on... He ...

Can you help me figure out what is causing an issue in my code? Any tips on removing a collection from MongoDB

I'm currently facing an issue with deleting a collection from MongoDB using the Postman API. The update function in my code is working perfectly fine, but for some reason, the delete function is not working as expected. It keeps displaying an internal ...

The JSON data structure is not being maintained

I am facing an issue with updating the json object model values using the code below. Even after changing the values, it seems like the model is not getting updated. I tried removing the async code and that worked. Why does the async code not work in this ...

Mastering the Art of Scrolling Down Content with Button Click in Ionic 3

I'm currently developing an Ionic chat application and I need the content to automatically scroll down when the user clicks on the send text button. You can see a visual representation of this in the images linked below. https://i.stack.imgur.com/gwR ...

The method firebaseApp.auth does not exist in user authentication using Firebase

Implementing user authentication with Firebase in my React webapp has been a challenge due to issues with the firebaseAuth.app() function. Despite trying various solutions such as reinstalling firebase dependencies, updating imports to SDK 9 syntax, and ad ...

Create an AngularJS directive that appends additional content after an element and allows the element to be

Looking to create a simple directive that triggers confirmation on button click. Here's the HTML setup: <button kr-confirm>Delete</button> Desired compilation: <button kr-confirm>Delete</button> <span ng-show="vm.isOpen" ...

Exploring different pages in an Ionic and AngularJS mobile application

I am brand new to the world of Ionic and AngularJS. I have just started working on a simple project but have hit a roadblock. My goal is, To create a login page and a register page. When a user clicks the register button on the login page, they should be ...

Show divs in identical position when clicking

There are 4 section divs that need to be displayed in the center of the page when clicked, but the last one appears further down. This is likely due to the flex box nature. What is the best way to ensure all sections appear at the exact same location? Ano ...

The backbone view is having trouble processing the data from this.model.toJSON()

I've been working on a Backbone code implementation to display all modifications before adding data to my model. However, every time I try to add something from my form, my view returns "this.model.toJSON() is not a function" and I can't figure o ...

What is the purpose of using JSON.parse(decodeURIComponent(staticString))?

A specific approach is utilized by some dynamic web frameworks in the following code snippet <script> appSettings = JSON.parse( decodeURIComponent( "%7B%22setting1%22%3A%22foo%22%2C%22setting2%22%3A123%7D")); </script> Is there a part ...

Unveiling the power of Axios and Vue in fetching API data: The quest for

I've encountered a problem while trying to integrate my API with Vue/Axios. The issue arises when I attempt to store the data retrieved by Axios into an empty variable within the data object of my component. It throws an "undefined at eval" error. Can ...

The development mode of NextJS is experiencing issues, however, the build and start commands are functioning normally

Just finished creating a brand new Next app (version: 12.0.7) using Typescript and Storybook. Everything seems to be working fine - I can successfully build and start the server. However, when I try to run dev mode and make a request, I encounter the follo ...

Is there a way to combine several WAV audio blobs into one cohesive file?

Issue I'm Facing: I am attempting to combine multiple blob audio files into a single blob and then download it on the page. My Attempts So Far: I have tried to merge the Audio blobs using the following methods: Method - 1: const url = window.URL.c ...

AngularJS - "Select All" elements that are currently within the view

Currently, I am faced with the challenge of selecting only the visible items in a list. The list is long and has paging applied to it, meaning only a few items are displayed at once. I have implemented a "Select All" button that should only select the curr ...

Invalidating the memory in iOS 7.1.1 when using canvas drawImage

When I use the following code on an animation frame, I notice a significant memory leak that eventually causes IOS Safari or Chrome to crash: ctx.drawImage(anotherCanvas, clipX, clipY, clipW, clipH, x, y, w, h); Interestingly, if I don't apply a ...

Dealing with extended render times in React applications

Currently, I'm working with a limited set of 100 documents per page and utilizing a wrapper component for certain conditional actions. const onClickCheckbox = (order: OrderProps) => { const _ordersToExport = [...ordersToExport]; const ind ...

Struggling to get Django and AngularJS to play nice?

After opening the file angular.html with AngularJS in Chrome, the table displays the array of information as expected. However, when attempting to use Django with the same angular.html file, the array is not showing up in the table. Unsure of how to procee ...

Tips for eliminating empty trailing values and Carriage Returns from a JavaScript array

I needed a way to eliminate empty elements and Carriage Returns from the end of an array. Here's an example of what my array looks like: Input arr: ['', 'Apple', '', 'Banana', '', 'Guava', & ...

Display an "add to cart" button and a discount image when hovering over

Currently, I am in the process of developing an Online Shopping website using PHP. To enhance the design of my website, I have implemented bootstrap. One specific query I have is how to display an 'add to cart' button and a discount image when a ...

Working with JSON in AJAX with Javascript and C# to handle array data

When attempting to send an array via AJAX using JSON, I am encountering a problem where my C# handler is unable to properly handle the query. It seems that the querystrings are merging together inexplicably. In the scenario presented here, I am trying to ...