var getting = { url:$scope.form.fileUrl, dataType:'text', success:function(res) { if(res != ""){ clearInterval(interval); var s = res.indexOf("<BODY"); var e = res.indexOf("</BODY>"); var re = res.substring(s,e); re = re.replace("<BODY","<"); $scope.form.changeEditor(re,true); } } } var interval = window.setInterval(function(){$.ajax(getting)},1000);