Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 14th, 2012  |  syntax: None  |  size: 0.87 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var http = require('http');
  2.  
  3. var numResults = 3,
  4.     numIteration = 3;
  5.  
  6. function fetchColors(offset, numResults, successHandler) {
  7.  
  8.   http.get({
  9.     host: 'www.colourlovers.com',
  10.     port: 80,
  11.     path: '/api/palettes/top?format=json&numResults=' + numResults + '&resultOffset=' + offset
  12.   }, function(res) {
  13.  
  14.     var body = "";
  15.  
  16.     res.setEncoding('utf8');
  17.     res.on('data', function (chunk) {
  18.       body += chunk;
  19.     });
  20.  
  21.     res.on('end', function () {
  22.       successHandler(JSON.parse(body));
  23.     });
  24.   }).on('error', function(e) {
  25.     console.log("Got error: " + e.message);
  26.   });
  27. }
  28.  
  29.  
  30. var i = 0;
  31.  
  32. function doIteration() {
  33.  
  34.   fetchColors( i * numResults, numResults, function (items) {
  35.     items.forEach( function (item) {
  36.       console.log(item.title);
  37.       console.log(item.colors);
  38.     });
  39.     if (i < numIteration) {
  40.       i++;
  41.       doIteration();
  42.     }
  43.   });
  44. }
  45.  
  46. doIteration();