AJAX has caused the webpage to freeze, prompting a warning about potentially slowing down the browser

My website features an AJAX call to the Alpha Vantage API, which retrieves JSON data on stock symbols and tickers as users type in the search box.

function handleKeyPress() {
	var keywords = document.getElementById("TextBox89").value;
	if (keywords.length >= 2) {
		var settings = {
			"async": true,
			"crossDomain": true,
			"url": "https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=" + keywords + "&apikey=B8ARVH4ULKOPMDOT",
			"method": "GET"
		}

		$.ajax(settings).done(function (response) {
			var keywordsArray = [];
			var json = response["bestMatches"];
			for (var i = 0; i < json.length; i++) {
				keywordsArray.push(json[i]["1. symbol"] + " - " + json[i]["2. name"]);
			}
			$("#TextBox89").autocomplete({
				source: keywordsArray
			});
		});
	}
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
Enter stock symbol/ticker: <input id="TextBox89" onKeyPress="handleKeyPress()" onKeyUp="handleKeyPress()">

In the code snippet above, the stock symbols and company names are retrieved using the jqueryui autocomplete() method, as demonstrated in this CodePen: https://codepen.io/max-voisard/pen/jOPERRd.

Despite the functionality in the CodePen, the implementation on my website's webpage results in browser freezing and unresponsiveness. This issue, which occurs when interacting with the search box, hints at a problem with the AJAX request's asynchronicity or potential overload on the webpage's resources.

While I acknowledge the security lapse of storing the API key in client-side JavaScript, the nature of the autocomplete() method makes server-side implementation challenging. Additionally, the API key in use is not sensitive.

Answer №1

When dealing with requests for large amounts of data, it's important to adjust the timeout setting in the AJAX call to allow enough time for the browser to retrieve the data. By simply including "timeout": 1000000 in the AJAX call, I was able to solve the issue. Surprisingly, there is no default timeout set for AJAX calls, so it's crucial to always set timeouts, especially for bulk data requests, to prevent issues like this. Additionally, ensure that the cache is set to false to avoid excessive memory allocation.

$.ajax({
    "timeout": 1000000,
    "cache": false
});

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 AngularJS to manage cookies along with arrays

I am passing my data in this way $cookies.putObject("currentLocation,values,allLocList", obj, vm.tempData, vm.allLocationList); The objects obj and vm.tempData are being sent as objects, while vm.allLocationList is an array that contains a JSON object. W ...

using selenium to fetch the static id value from an ajax request

How can I retrieve the value of an option using AJAX? Below is the HTML code: <select id="j_id0:searchlayout:mainform:countryVal" name="j_id0:searchlayout:mainform:countryVal" size="1" onchange="A4J.AJAX.Submit('j_id0:searchlayout:mainform',e ...

Aligning Font Awesome icons inside md-buttonsFinding the perfect alignment for

I am attempting to center font awesome icons within a button so that they align perfectly with the text on a toolbar. Below is the code snippet I am working with: <div ng-app="app"> <md-toolbar> <div class="md-toolbar-tools" md-tall ...

Optimize your mobile experience by creating a notification menu that is scrollable and fixed in position

Recently, I purchased Moltran, but encountered a major issue: The notification menu disappears on mobile devices, which is not ideal for me. After some research, I discovered that removing the hidden-xs class from the li notification element can solve this ...

Having Trouble Finding Vue Component Definition Using Vite Alias in Import Path

When I click on the Component within the code snippet: import Component from '@/components/Component.vue'; I am unable to navigate to its definition when using vite alias. Seeking a potential solution. ...

Problems arise with identification of asynchronous functions

My async functions have been used successfully in various projects, but when incorporating them into a new project, they suddenly stopped working. One of the functions causing trouble is: const ddbGet = async (params) => { try { const data ...

What could be causing the "undefined property read" error even though the data is clearly defined?

export async function getPrices() { const res = await fetch( "https://sandbox.iexapis.com/stable/crypto/BTCUSD/price?token=Tpk_1d3fd3aee0b64736880534d05a084290" ); const quote = await res.json(); return { props: { quote } }; } export de ...

AngularJS: splitting the parent <div> into multiple sections every nth element

I have an array of strings in Javascript and I am attempting to use AngularJS to create nested <div> elements. var arr = ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu"]; My goal is to group every 3 elements together like so. <div class="pare ...

Tips for horizontally arranging a list with a flexible number of columns

I am attempting to create a horizontal list with 3 columns that automatically moves to a new line when the columns are filled. I have been trying to implement this using PHP but haven't had any success. If anyone could provide some guidance on how to ...

Issue with the compatibility of the datepicker and autocomplete features in jQuery

Separately, the code below works on different PHP pages. However, when combining them on one page, nothing seems to work. In Firebug, the error message reads: TypeError: $(...).datepicker is not a function $("#test1").datepicker({ DATEPICKER CODE & ...

Reload a forum in a div without refreshing the entire page

I'm interested in finding out if it's possible for a div to reload similar to how a window does. I have a forum set up, but I don't want the entire page to reload after each form submission. Instead, I'm wondering if only the elements w ...

Spring MVC controller calling method twice results in JQuery request being aborted

I've been struggling with this issue for the past three days and haven't been able to find any useful information online. Here is the problem I'm facing: I have a .jsp page that shows a list (in my controller, handled by @RequestMapping(val ...

Template for Vue.js Component Registration

Below is a simple Vue component example: const MyComponent = Vue.component('my-component', { data () { // data here... }, methods: { // methods here... }, template: '<p>Hello, world !!!</p>' }); I ...

An element generated using a JavaScript loop is covering another element in the layout

I am facing an issue with positioning images within a div to a span. The problem arises as the images are overlapping each other and I am uncertain about how to properly place each image when it is added. Below is the code snippet: The CSS: <style ty ...

Transform a single data point into pixels by converting its latitude and longitude coordinates

I am facing a specific challenge where I have hit a roadblock after conducting some research. The issue at hand is this: I am working with a raster image that has known dimensions (800 x 800 px) I have two points within this image with their pixel and g ...

JavaScript prototypal inheritance concept

During my free time, I like to dabble in JavaScript, but I’m currently struggling with this particular topic. var person = new Person("Bob", "Smith", 52); var teacher = new Teacher("Adam", "Greff", 209); function Humans(firstName, lastName) { this. ...

option for uploading various data to the database using (php, ajax)

I am using a foreach loop to display data from the database on the screen. However, when I click the button, I do not see any results. Can someone please help me identify where I may be making mistakes? <table class="table"> <thead class="the ...

The functionality of the Eslint object-property-newline feature is not functioning as expected

Within my configuration file .eslintrc, I have set "object-property-newline": ["error", { "allowAllPropertiesOnSameLine": true }], and "max-len": "off". However, objects like const something = {a: 5, ffsdfasdasdsddddd: 'asdasdasdddddddddddssssssddddd ...

learning how to combine two json arrays of objects and showcase them in a react component

What is the best way to combine this data and present it in a table with a map using React? The text will be in the first column and the count in the second. const handleSubmit = async (event) => { event.preventDefault(); let URL1 = " ...

Internal server error frequently occurs when there is an issue with Ajax requests in a Laravel application

Greetings, fellow developers! I am encountering an issue with the comments system in Laravel and Ajax. While it functions correctly with PHP alone, I am facing difficulties when using Ajax. The error message I am receiving is as follows: Status Code:50 ...