# Wait time between API calls with Power Query



## Kazlik (Jun 25, 2015)

I have the following function I pass many URL's to doing this #"Added Custom" = Table.AddColumn(#"First Row as Header", "Custom", each SharedCount ()),

I would like to add in a wait ...l="&URL&""))
in
     Source
in
     GetCounts


----------



## scottsen (Jun 30, 2015)

I'll get Ken Puls or Chris Webb to come answer.  Will page them on twitter


----------



## Ken Puls (Jun 30, 2015)

Try this:


```
let
GetCounts = (URL as text)=>
let
    //Define Wait function
    Wait = (seconds as number, action as function) => 
            if (List.Count(
             List.Generate(
              () => DateTimeZone.LocalNow() + #duration(0,0,0,seconds), 
              (x) => DateTimeZone.LocalNow() < x, 
              (x) => x)
              ) = 0) 
               then null else action(),
    //Pause x seconds between calls
    Pause = Wait(.25,DateTime.LocalNow),

    //Request data from URL
    Source = Json.Document(Web.Contents("https://free.sharedcount.com/url?apikey=myapikey&url="&URL&""))
in
Source
in
GetCounts
```


----------



## Kazlik (Jun 30, 2015)

Thank you Ken this helps a lot.


----------



## ChrisWebb (Jun 30, 2015)

Function.InvokeAfter() should work too. Can you try the following please?




```
(URL as text)=> 
Function.InvokeAfter(
()=>Json.Document(Web.Contents("https://free.sharedcount.com/url?apikey=myapikey&url="&URL&"")),
#duration(0,0,0,2))
```

Chris


----------



## Kazlik (Jul 1, 2015)

Thank you Chris and Ken for all your help with this problem. Both implementations worked and now I have a wait time between my API calls.


----------



## Ken Puls (Jul 1, 2015)

ChrisWebb said:


> Function.InvokeAfter() should work too.



Nice Chris, I like that!


----------

