im using the zombie.js library , to do some testing for the dom, i have this script
Browser = require "zombie"
arr = new Array("http://yahoo.com", "http://google.com", "http://msn.com")
i = 0
while i < arr.length
b = new Browser()
b.visit arr[i], ->
console.log b.text "title"
b.close()
i++
everything works, but it only logs the msn title two times, so the while loop is logging the last item in the array twice. i can’t seem to see what the problem is?
You’re registering a callback for when the browser’s
visithas completed. By that time, the loop has run to completion, so the variablebpoints to the last browser you created, which means its title will be that of MSN, not those of the other pages. In order to fix this, use a closure:This works because now there is a separate scope for each browser you create, and a separate browser variable.
Alternatively, use the callback arguments for ZombieJS’s
visitfunction: