How to set a callback function in javascript

时间:2017-12-18 07:37:47

标签: javascript function callback

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.

<StackLayout Orientation="Horizontal" IsClippedToBounds="false" Spacing="5"  >
     <Button Text="ABCDEFG" TextColor="Blue" BackgroundColor="Transparent" WidthRequest="90" HeightRequest="30" ></Button> 
     <Button Text="ABCDEFG" TextColor="Blue" BackgroundColor="Transparent" WidthRequest="90"  HeightRequest="30" ></Button> 
     <Button Text="ABCDEFG" TextColor="Blue" BackgroundColor="Transparent" WidthRequest="90"  HeightRequest="30" ></Button> 
     <Button Text="ABCDEFG" TextColor="Blue" BackgroundColor="Transparent" WidthRequest="90"  HeightRequest="30" ></Button>  
</StackLayout>

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

setTimeout(function a (){
   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.

4 个答案:

答案 0 :(得分:0)

<import type="com.myapp.R" /> android:textColor="@{selected ? R.color.white : R.color.turquoise}" will in fact call your function after the timeout. Just call your function setTimeout instantaneously and pass a() to b.

Example:

setTimeout

Hope that helped!

答案 1 :(得分:0)

What about this:

db.collection.aggregate([{$project :{numstring : {$substr :["$number",0,12]}}},{$match :{"numstring":{$regex : /^8.5/}}}])

Or do you need something like this:

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

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

答案 2 :(得分:0)

shorthand version of the IP address

This calls function a and 200 milliseconds later calls function b

答案 3 :(得分:0)

You could use JavaScript Promises to execute function Public Function ColorRYG(ByVal Value As Decimal, ByVal MaxPositive As Decimal, ByVal MaxNegative As Decimal, ByVal Neutral As Decimal) As String 'Example: =code.ColorBack(expression, Max(expression), Min(expression), 0) '=code.colorback( Fields!Sales.Value,max( Fields!Sales.Value),min( Fields!Sales.Value),0) 'Find Largest Range Dim decRange As Decimal Dim decPosRange As Decimal = Math.Abs(MaxPositive - Neutral) Dim decNegRange As Decimal = Math.Abs(MaxNegative - Neutral) decRange = IIf(decPosRange > decNegRange, decPosRange, decNegRange) 'Force color into Max-Min Range. Important if you want to Clip the color display to a subset of the data range. Value = Switch((Value > MaxPositive), MaxPositive, Value < MaxNegative, MaxNegative, True, Value) 'Find Delta required to change color by 1/255th of a shade Dim decColorInc As Decimal = 255 / decRange 'Find appropriate color shade Dim iColor As Integer = CInt(Math.Round((Value - Neutral) * decColorInc)) 'Return Appropriate +ve or -ve color Dim strColor As String If iColor >= 0 Then 'Green iColor = 255 - iColor 'Thus 0 = White & 255 = Green strColor = "#" & iColor.ToString("X2") & "FF00" Else 'Red iColor = iColor + 255 'NB iColour is -ve; -1 - -255 strColor = "#FF" & Math.Abs(iColor).ToString("X2") & "00" End If Return strColor End Function once function b done executing.

a
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;
}