Explain $timeout and $interval and potential pitfalls in AngularJS.

$timeout and $interval are Angular wrappers for setTimeout and setInterval, integrated with the digest cycle. They ensure model updates are reflected in the DOM. To prevent memory leaks, use $timeout.cancel() and $interval.cancel() for clean-up, especially for ongoing tasks.

var interval = $interval(function() {
  console.log("Repeating task");
}, 1000);

// Cancel the interval
$interval.cancel(interval);