Harry Singh
Harry Singh

Reputation: 423

Do While Loop with a timed interval

I am trying to refresh a search page with a new search every time.

$i = 0
do {
   $srch = Get-Random
   Start-Process "http://www.swagbucks.com/?t=w&p=1&b=0&f=8&sef=0&q=$srch"
   $i++
}
while ($i -lt 5)

This opens a new tab with a new link. I want to be able to open the link in the same tab and run through it at timed intervals.

Upvotes: 0

Views: 1192

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You don't want to refresh a page, you want to load a new page with every iteration (as the query part of your URL changes with every iteration).

$delay = ...

$ie = New-Object -COM 'InternetExplorer.Application'
$ie.Visible = $true

$i = 0
do {
  $srch = Get-Random
  $url  = "http://www.swagbucks.com/?t=w&p=1&b=0&f=8&sef=0&q=$srch"

  $ie.Navigate($url)

  do {
    Start-Sleep -Milliseconds 100
  } until ($ie.ReadyState -eq 4)

  $i++

  Start-Sleep -Seconds $delay   # wait until next iteration
} while ($i -lt 5)

If you want to do a given number of iterations, a for loop might be preferrable to a do loop, though:

for ($i = 0; $i -lt 5; $i++) {
  $srch = Get-Random
  $url  = "http://www.swagbucks.com/?t=w&p=1&b=0&f=8&sef=0&q=$srch"

  $ie.Navigate($url)

  do {
    Start-Sleep -Milliseconds 100
  } until ($ie.ReadyState -eq 4)

  Start-Sleep -Seconds $delay   # wait until next iteration
}

Upvotes: 1

Related Questions