The JavaScript setInterval function does not have the ability to automatically repeat the task

I am attempting to use the setInterval function to fetch data from another website every 10 seconds. However, it is only running for the first time and then stopping.

setInterval(fetchData("testtest"), 10000);

function fetchData(username){  
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
   if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
      var jsonData= JSON.parse(xhttp.responseText);
      console.log(jsonData.total);
      console.log(username); 
    }
  };
  var url="https://hypothes.is/api/search?user=" + username + "&sort=created&order=asc";
  xhttp.open("GET", url, true);
  xhttp.send();
  alert("test");
  }

Answer №1

setInterval(function(){evaluate("checkcheck")}, 10000);

Answer №2

setInterval requires a function to be passed in as an argument. In this case, you mistakenly passed the result of the hypothes function instead. Here's how you can correct it:

setInterval(function() { hypothes("testtest") }, 10000);

Answer №3

It looks like the issue lies in how your code is currently structured. Instead of properly setting your function as a callback, you are invoking it immediately.

To fix this problem, make the following adjustment to the section where you define the interval:

setInterval(function() { analyze("example"); }, 10000);

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

Prevent the use of exponential notation with double numbers in GWT

Is there a way to remove the exponent from a double on the client side in GWT? public double evaluate(final double leftOperand, final double rightOperand) { Double rtnValue = new Double(leftOperand * rightOperand); //Need code to remove expone ...

Angular 10 does not fulfill promises as expected

In the Angular 10 project I'm working on, I encountered an issue while trying to call an API request asynchronously using promises. The code I wrote didn't seem to execute the API call as expected and kept exiting at the first line without progre ...

The functionality of the Javascript window.print() method is limited to a single use

I've been working on an Angular project and I have the following code snippet implemented in one of the components. Everything works fine when I try to print for the first time using the onClickPrint() method, but it doesn't seem to trigger when ...

Struggling with synchronicity in javascript(node.js) and seeking assistance

I am faced with a challenge in my express.js app where I need to execute a script on the server, followed by running a couple of functions to derive some values. The process should be sequential, but I am running into issues as JavaScript seems to move on ...

Having trouble with a beginner problem that's hindering the functionality of my code

I have been struggling with a particular piece of code and it is driving me crazy as I am unable to locate the source of my error: $.post($form.attr('action'), $form.serialize(), function (result) { console.log(result); if (result.succes ...

Incorporating external CSS and JS files into your WordPress website

Hello, I am unfamiliar with the Wordpress framework and I am seeking guidance on how to add an external CSS and JS file to my Wordpress page. I have successfully created a new page, but would like to incorporate a CSS and JS file into it. Would creating a ...

Bootstrap form validation issues

Currently, I am utilizing Vue.js along with Bootstrap for the creation of a website. In this process, I have set up a form on which I am implementing my custom validation logic. Everything seems to be functioning correctly until the moment when the user hi ...

Developing client-side components with NextJS

I want to develop a single-page landing page where users can upload videos and there's a file size limit check before the upload. In my src/app/page.tsx file, I have the following code: import React from 'react'; import FileUpload from &apo ...

Modify the content within the <h2> Tag that needs updating with JavaScript

Looking for guidance on how to update the text within an HTML element using JavaScript. Any suggestions? The current setup is as follows: <h2 id="something">Text I want to change.</h2> I attempted to achieve this with: document.getElemen ...

Encountering issues with loading a module in Aurelia

I've encountered a peculiar issue while attempting to load a module using Aurelia. The moment library was successfully loaded for date formatting, but when trying to load the numeral library in the same manner with npm install <module> --save, i ...

Using PHP to perform live calculations with arrays in real time

Currently, I am working on developing a new system for a client that allows them to create bookings and invoices to send to their clients. One specific requirement my client has is the need for a table column to be added below the container columns in whi ...

Which jquery Grid is capable of handling a constant flow of updates in json format?

Provided below is a brief summary of this extensive post (please read the entire post to provide an answer): Current Implementations: The website retrieves a large json dataset (approximately 30kb, already compressed) The data is rendered as an HTML tab ...

Using Alpine JS to rotate through images at regular intervals using the window.setInterval() method

Attempting to tackle a simple task using Alpine JS and the standard JS function setInterval. The goal is to create an image selector where images switch every second (1000ms). Here's what I have so far: <div x-data="imgFunc()"> ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Remove the initial x characters from a numerical value and verify if it aligns with a specified regular

I need to remove the initial 6 characters (numbers) from a number and verify if it matches any number on a given list. For instance, if we have a number input like: 1234567891234567 The first 6 characters extracted would be: 123456 Then I want to confi ...

Having trouble retrieving the data from a post request in Express

I'm encountering an issue while attempting to access the data in a POST request. Even after using console.log(req.body), I only receive an empty {}. My suspicion is that the error may lie somewhere within the markup? Below is the relevant client-sid ...

Unable to set cookies properly

I am currently running a svc service on crm.mch.be. When a specific request is made, it returns a response with the following headers: Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: X-Requested-With,Content-Type Access-Control-Allow- ...

Unexpected behavior: Axios post still enters catch block despite successful completion of Rest API call

Having an issue with my axios post request not returning the value from the API in a non-success scenario (401 error). It works fine for successful scenarios. When using Postman to test the output of my reset password API by providing an incorrect current ...

Unable to sort nested JSON data in ngTable

I have successfully built an angularjs application using ngTable, however, I am facing an issue with sorting. The JSON structure is nested but the values are appearing correctly in the table. If anyone has a solution to this problem, please let me know. ...

Concerns about the Dependency Tree in React

I need some assistance with my current issue. I'm having trouble installing the mui search bar component. npm i --save material-ui-search-bar Unfortunately, I'm encountering this error message: PS Z:\WebDev\ApplyWithin\frontend> ...