0

I want to excecute function b after the setTimeout function a has finished. I allready know i need a asynchronous callback function for that but have problems to realise that.

setTimeout(function a (){
   element.classList.add('classNameA');
},200);

function b () {
   element.classList.add('classNameB');
}

I tried this one but it doesn't work, what is wrong with that?

setTimeout(function a (b){
   element.classList.add('classNameA');
},200);

function b () {
   element.classList.add('classNameB');
}

EDIT: The function a setTimeout is needed because of an previous transition duration. Function b should get excecuted immediately after function a has done its work.

EDIT: i made my example more clear - i have two different classes i have to add.

1

4 Answers 4

0

What about this:

function b() {
   element.classList.remove('className');
}

setTimeout(function a(){
   element.classList.add('className');
   b();
},200);

Or do you need something like this:

setTimeout(function a(){
   element.classList.add('className');
   setTimeout(function b() {
       element.classList.remove('className');
   }, 200);
},200);
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't sample b excecute function b synchronous? I need it to get started after function a is done and i don't want a second setTimeout function for that.
I don't really get what do you want to do. Yeah the first block will execute function b() synchronously after the function a(). I think you should try with promise, look at this
Okay i will take a look on that. Thanks
0
 function a (){
   element.classList.add('className');
   setTimeout(b,200);
 }

 function b () {
   element.classList.remove('className');
 }

 a(); 

This calls function a and 200 milliseconds later calls function b

5 Comments

Thanks for your sample, but it doesn't fit to my situation. I edited my Question.
Function a() happens immediately and the animation would happen for 200ms right?
Yep, you're right, but function a has to wait for the animation duration of 200ms and function b should excecuted immediately after this.
In the above, function a gets executed and className exists for 200ms after which it is removed.
Ah okay now i understand why my Question is confusing - i have to different classes, it's not the same that gets added and removed.
0

You could use JavaScript Promises to execute function b once function a done executing.

var element = document.querySelector('.element');
function a() {
   return new Promise(function(resolve, reject) {
     setTimeout((function() {
		 console.log('class added');
		 resolve(element.classList.add('red')); 
    }), 2000);
});
}
function b() {
	 console.log('class removed..');	
     element.classList.remove('red');
}
a().then(b);
.red{
	color: red;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>SetTimeout</title>
</head>
<body>
<span class="element">hello</span>
</body>
</html>

Comments

0

setTimeout will in fact call your function after the timeout. Just call your function a() instantaneously and pass b to setTimeout.

Example:

 function a (){
   element.classList.add('className');
 }

 function b () {
   element.classList.remove('className');
 }

 setTimeout(async function (){
   await a();
   b();
 },200); // a is called after 200 milliseconds

Hope that helped!

6 Comments

Thanks for your sample, but it doesn't work for me. The function a has a timeout because of an transition, after which the function have to get excecuted. The problem is, that i want function b excecuted immediately after the function a is done, without an additional setTimeout function.
Got it! Hope you are trying to call both a and b one after one, after specified time without additional timeout?
correct! But i think in your sample a and b would be excecuted synchronous, or not?
Yes, first a will be called then b
Okay, but i need it asynchronous, in youre example both would be excecuted simultaneously, but i need b after a is done.
|

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.