Search code examples
javascriptjquerylaraveltimeoutsynchronous

TypeError: $.doTimeout is not a function


my code is the following:

$("p").click(function(){
  $(this).hide();
})

  $.doTimeout('myLoop', 5000, function() {
      ++i;
        timeleft = calctime(id, jsondata);
        document.getElementById("timeleft").innerHTML = timeleft;
      /* From the docs: "If the callback returns true, the doTimeout loop will execute again, after the delay, creating a polling loop until the callback returns a non-true value." */
      return i == 50 ? false : true;
  });
};

the JS is implemented in my laravel server so maybe that is causing problems.

I've tried the following things:

1 download the jquery file instead of using the google service
2 add a different jquery function (the top part of the code) to see if jquery works correctly. which it does.

My goal is to make a loop that has a synchronous delay (unlike javascripts asynchronous setTimeout) so the code runs through the loop once every 5 seconds.

Jquery is working since the top piece of my code is functioning correctly.

the Jquery i am using is version 3.3.1 on Mozilla Firefox

can anyone provide some information why doTimeout isn working?

Alright so i made this snippet but here i don think the doTimeout library is working properly since i get two errors now. hope it helps

var i = 0;
$.doTimeout( 100, function(){
  i++;
  document.getElementById("counter").innerHTML = i;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-dotimeout/1.0/jquery.ba-dotimeout.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="counter"></div>


Solution

  • I made a workaround for the problem without using doTimeout.

    function fuckit(timeleft, id, jsondata){
      setTimeout(function(){
    
          timeleft = calctime(id, jsondata);
          document.getElementById("timeleft").innerHTML = timeleft;
          fuckit(timeleft, id, jsondata);
      }, 1000);
    }
    

    its basicly a function call within itself.

    I wouldn't recommend anyone to do this because it's basicly bad coding but it works and since its only a school project i'm satisfied with it.