1

I have two functions for eg., runslider() and runslider1(). runslider() runs after the document is loaded and I need to call runslider1() after finishing runslider(). Then again runslider() after runslider1(). This process should happen like infinite loop. Can someone help me please. I have tried to keep them like callbacks. But that didn't work.

    function runSlider(runslider1){
        alert("run")
        runSlider1(runSlider());
    }

    function runSlider1(runslider){
        alert("run1");
        runSlider(runSlider1());
    }
5
  • 10
    If you do that, it will result in a stack overflow Commented Aug 15, 2017 at 17:22
  • 1
    Just create an infinite loop calling these two functions. Commented Aug 15, 2017 at 17:24
  • Remember that JavaScript doesn't allow multithreaded execution. So this hangs the browser. However as said, setInterval will do the trick. Commented Aug 15, 2017 at 17:27
  • 2
    The code is wrong and we can fix it, but why would you want to break your browser in the first place? Commented Aug 15, 2017 at 17:27
  • you only need one function to make it loop and here's what happens: jsfiddle.net/g16csqod Commented Aug 15, 2017 at 17:30

5 Answers 5

2

if you want your functions to be called over and over again try using setInterval

function runSlider(){
    alert("run");
    runSlider1();
}
function runSlider1(){
    alert("run1");
}
setInterval(runSlider, 100);

This will cause both functions to be called in that order repeatedly every 100ms. It seems like this is the behavior you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

0

The comments above are correct - you will cause a stack overflow.

Don't know why you would need this, but I cleaned your code for you:

function runSlider() {
    alert('run');
    runSlider1();
}

function runSlider1() {
    alert('run1');
    runSlider();
}

Comments

0

You can create infinite loop like this you just need to call one function.

var runSlider = function() {
  console.log("run")
  runSlider1()
}

var runSlider1 = function() {
  console.log("run1");

  setTimeout(function() {
    runSlider()
  }, 1000)
}

runSlider()

Comments

0

Another solution is:

function runSlider() {
    console.log("run");
    runSlider1();
    setTimeout(runSlider1(), 1000) // Calls runSlider1() after 1 second(1000 millisecond). You can change it.
}

function runSlider1() {
    console.log("run1");
    setTimeout(runSlider(), 1000) // Calls runSlider1() after 1 second(1000 millisecond).
}

runSlider(); // Starts the cycle

1 Comment

this gives an error 'runSlider1' was used before it was defined.
0
var maxCalls = 0;

function run1(cb) {
  alert('run1');
  if (maxCalls++ < 5) { //Added this to avoid an infinite loop
    cb(run1); //We want the function run after cb to be this one
  }
}

function run2(cb) {
  alert('run2');
  if (maxCalls++ < 5) {
    cb(run2);
  }
}

This is the way to call one function from another. If you create an infinite loop, you will freeze the browser up. If you want the two functions running constantly, its best to release execution for a bit with a setInterval call instead.

var runFunc = 0;

var run1 = function() {
  alert('run1');
};

var run2 = function() {
  alert('run2');
};

var run = function() {
  !(++runFunc) ? run2 : run1; //Alternate between calling each function
}

var stopRunning = function() { //Call this to stop the functions running
  clearInterval(runInterval);
};

var runInterval = setInterval(run, 1000); //Calls the function every second

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.