Here an example of one way to do it. This is based off the code found here
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Intro to MapView - Create a 2D map - 4.7</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.7/esri/css/main.css">
<script src="https://js.arcgis.com/4.7/"></script>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/core/watchUtils",
"dojo/domReady!"
], function(Map, MapView, watchUtils) {
let initialExtent;
const map = new Map({
basemap: "streets"
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 8,
center: [-77,39]
});
watchUtils.whenFalse(view, 'stationary', function (evt) {
if (!view.stationary) {
watchUtils.whenTrueOnce(view, 'stationary', function (evt) {
checkExtent(view.extent);
});
} else {
watchUtils.whenFalseOnce(view, 'interacting', function (evt) {
console.log("interacting", view.extent);
});
}
});
function checkExtent(newExtent) {
console.log('Checking extent...');
if (!initialExtent) {
initialExtent = view.extent;
}
const currentCenter = view.extent.center;
if (initialExtent && !initialExtent.contains(currentCenter)) {
let newCenter = view.extent.center;
if (currentCenter.x < initialExtent.xmin) {
newCenter.x = initialExtent.xmin;
}
if (currentCenter.x > initialExtent.xmax) {
newCenter.x = initialExtent.xmax;
}
if (currentCenter.y < initialExtent.ymin) {
newCenter.y = initialExtent.ymin;
}
if (currentCenter.y > initialExtent.ymax) {
newCenter.y = initialExtent.ymax;
}
view.goTo(newCenter, {
duration:1000
});
console.log('Outside original extent...');
}
}
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>