Click here to Skip to main content
15,885,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call this function every 5 seconds. But I get error setTimeout is not a function

What I have tried:

    $(document).ready(function () {
         started = true;
         var total = line.length;

         line.forEach(setTimeout(function(value) {
         var ajaxCall = $.ajax({
         url: url,
         type: 'GET',
         data: some data,

    success: function(data) {
    //some codes here

        doneCheck();
}

            });
            $('#stop').click(function () {
                ajaxCall.abort();
                $('#start').attr('disabled', null);
                $('#stop').attr('disabled', 'disabled');

            });
        }, 5000)); // 5 seconds timeout
  });
Posted
Updated 13-Jul-22 0:20am

The easiest solution would be to have the timeout registered as part of each ajax request. The problem you're having is that you're calling setTimeout() within the forEach method directly which isn't a valid process. Consider something like the below:
var running = true;

function update(value)
{
  // If the variable has been set to anything but true
  // then we know the updates have been cancelled
  if (!running)
    return;

  $.ajax({
    url: url,
    type: 'GET',
    data: data,

    // The success function here is called only when the
    // request was successful
    success: (data) => {
      // Do some work here
    },

    // The complete function here is called even if the
    // request failed, guaranteeing that the timeout will
    // be set to run again
    complete: () => {
      setTimeout(() => update(value), 5000);
    }
  });
}

$(document).ready(function()
{
  // Call the update function direct against each line,
  // passing the value into the method
  line.forEach(update);
});

You can then stop the updates by setting the running variable to false (and you could alternatively track and cancel the timeouts, but for simplicity sake I haven't included that). To start the updates again you can simply set the running variable to true and loop over the lines again to call the update() function.
 
Share this answer
 
v2
lines.forEach expects to be passed a function which will be called for every item in the lines collection.

You are passing in the value returned from the setTimeout function, which is an integer. This cannot be used as a callback function parameter.

Your question says you want to call a function every five seconds. But setTimeout will only call a function once, after the specified delay.

I suspect you want something more like this:
JavaScript
$(function(){
    started = true;
    
    const interval = 5000; // 5 seconds
    let abortController = new AbortController();
    
    const checkLine = function(value){
        const ajaxCall = $.ajax({
            url: url,
            type: "GET",
            data: someData,
            success: function(data){
                abortController.signal.throwIfAborted();
                // some codes here
                doneCheck();
                setTimeout(function() { checkLine(value) }, interval);
            }
        });
        
        abortController.signal.addEventListener("abort", function(){
            ajaxCall.abort();
        });
    };
    
    const total = line.length;
    line.forEach(checkLine);
    
    $("#stop").click(function(){
        abortController.abort();
        $("#start").property("disabled", false);
        $("#stop").property("disabled", true);
    });
    
    $("#start").click(function(){
        abortController = new AbortController();
        $("#start").property("disabled", true);
        $("#stop").property("disabled", false);
        line.forEach(checkLine);
    });
});
 
Share this answer
 
add a index and set it's value and increment the index value in the loop. Also use async:false.
 
Share this answer
 
Comments
Richard Deeming 13-Jul-22 6:05am    
Synchronous XHR has been deprecated since 2014:
Synchronous and asynchronous requests - Web APIs | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900