Javascript 在时间间隔后调用函数

Javascript 在时间间隔后调用函数,javascript,html,angularjs,Javascript,Html,Angularjs,我试图在显示成功消息后刷新页面。我使用了很多代码片段,但要么它在不显示消息的情况下刷新,要么相反 所以我想在一段时间后调用Refresh函数,或者在一段时间后再次触发button click事件来刷新页面 下面是代码,如何实现上述场景 JS代码 HTML按钮调用 <div class="col-sm-offset-1"> <button id="PublishButton" class="btn btn-default shiny " ng-disabled="mani

我试图在显示成功消息后刷新页面。我使用了很多代码片段,但要么它在不显示消息的情况下刷新,要么相反

所以我想在一段时间后调用Refresh函数,或者在一段时间后再次触发button click事件来刷新页面

下面是代码,如何实现上述场景

JS代码 HTML按钮调用

<div class="col-sm-offset-1">
    <button id="PublishButton" class="btn btn-default shiny " ng-disabled="manifeststatus.enablePublishButton" ng-click="Save(manifeststatus)">Publish</button>
</div>

发表

这里我想做的是在一段时间间隔后调用Refresh1()函数,这样页面在显示消息后就会刷新。

注入$timeout并对JS代码进行更改

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        //Adding a 5 second delay
        $timeout($scope.Refresh1, 5000);

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});

注入$timeout并对JS代码进行此更改

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        //Adding a 5 second delay
        $timeout($scope.Refresh1, 5000);

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});

注入$timeout并对JS代码进行此更改

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        //Adding a 5 second delay
        $timeout($scope.Refresh1, 5000);

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});

注入$timeout并对JS代码进行此更改

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        //Adding a 5 second delay
        $timeout($scope.Refresh1, 5000);

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});
我有点了解您的问题,我不需要使用
$timeout
来使用更新的数据更新模型

.controller('AController', function(ptth$) {

    var self = this;

    self.time = 'wait for it';

    self.init = function() {
      ptth$.get().then(function(data) {
        self.time = data;
      });
    };

    self.init();

    self.save = function() {
        ptth$.post().then(function() {
          //I use an alert instead of a div we show or hide.
          window.alert('hey, this is real success');
          //You either re-fetch data here and update your model
          //Or you reload after the alert...

          //This example works with a ng-model update, instead of a reload
          self.init();
        }, function() {
          window.alert('this is failure');
        });
    };
})
尽管这不是我设置东西的方式,但这个例子应该说明,对于这一点,您并不真正需要
$timeout
$reload

我有点了解您的问题,我不需要使用
$timeout
用更新的数据更新模型

.controller('AController', function(ptth$) {

    var self = this;

    self.time = 'wait for it';

    self.init = function() {
      ptth$.get().then(function(data) {
        self.time = data;
      });
    };

    self.init();

    self.save = function() {
        ptth$.post().then(function() {
          //I use an alert instead of a div we show or hide.
          window.alert('hey, this is real success');
          //You either re-fetch data here and update your model
          //Or you reload after the alert...

          //This example works with a ng-model update, instead of a reload
          self.init();
        }, function() {
          window.alert('this is failure');
        });
    };
})
尽管这不是我设置东西的方式,但这个例子应该说明,对于这一点,您并不真正需要
$timeout
$reload

我有点了解您的问题,我不需要使用
$timeout
用更新的数据更新模型

.controller('AController', function(ptth$) {

    var self = this;

    self.time = 'wait for it';

    self.init = function() {
      ptth$.get().then(function(data) {
        self.time = data;
      });
    };

    self.init();

    self.save = function() {
        ptth$.post().then(function() {
          //I use an alert instead of a div we show or hide.
          window.alert('hey, this is real success');
          //You either re-fetch data here and update your model
          //Or you reload after the alert...

          //This example works with a ng-model update, instead of a reload
          self.init();
        }, function() {
          window.alert('this is failure');
        });
    };
})
尽管这不是我设置东西的方式,但这个例子应该说明,对于这一点,您并不真正需要
$timeout
$reload

我有点了解您的问题,我不需要使用
$timeout
用更新的数据更新模型

.controller('AController', function(ptth$) {

    var self = this;

    self.time = 'wait for it';

    self.init = function() {
      ptth$.get().then(function(data) {
        self.time = data;
      });
    };

    self.init();

    self.save = function() {
        ptth$.post().then(function() {
          //I use an alert instead of a div we show or hide.
          window.alert('hey, this is real success');
          //You either re-fetch data here and update your model
          //Or you reload after the alert...

          //This example works with a ng-model update, instead of a reload
          self.init();
        }, function() {
          window.alert('this is failure');
        });
    };
})


尽管这不是我设置东西的方式,但这个例子应该说明,对于这一点,您并不真正需要
$timeout
$reload

使用
$state.reload
启动
$scope.Refresh1
功能。如果您使用的是ui路由器。$timeout-如果您只需要在一段时间内显示一些警报,请使用
toast
库。为什么需要重新加载?要重新提取刚获取的数据?除此之外,当你显然不知道你的post操作是否成功时,你似乎在发布成功消息。另外,您似乎正在使用jQuery和Angular来显示消息。@skubski那么,可以为此做些什么呢?我提取了数据。但除非我手动刷新,否则它不会显示。所以我想在代码中刷新它。使用
$state.reload
来启动
$scope.Refresh1
函数。如果您使用的是ui路由器。$timeout-如果您只需要在一段时间内显示一些警报,请使用
toast
库。为什么需要重新加载?要重新提取刚获取的数据?除此之外,当你显然不知道你的post操作是否成功时,你似乎在发布成功消息。另外,您似乎正在使用jQuery和Angular来显示消息。@skubski那么,可以为此做些什么呢?我提取了数据。但除非我手动刷新,否则它不会显示。所以我想在代码中刷新它。使用
$state.reload
来启动
$scope.Refresh1
函数。如果您使用的是ui路由器。$timeout-如果您只需要在一段时间内显示一些警报,请使用
toast
库。为什么需要重新加载?要重新提取刚获取的数据?除此之外,当你显然不知道你的post操作是否成功时,你似乎在发布成功消息。另外,您似乎正在使用jQuery和Angular来显示消息。@skubski那么,可以为此做些什么呢?我提取了数据。但除非我手动刷新,否则它不会显示。所以我想在代码中刷新它。使用
$state.reload
来启动
$scope.Refresh1
函数。如果您使用的是ui路由器。$timeout-如果您只需要在一段时间内显示一些警报,请使用
toast
库。为什么需要重新加载?要重新提取刚获取的数据?除此之外,当你显然不知道你的post操作是否成功时,你似乎在发布成功消息。另外,您似乎正在使用jQuery和Angular来显示消息。@skubski那么,可以为此做些什么呢?我提取了数据。但除非我手动刷新,否则它不会显示。因此,我想在代码中刷新它。我尝试了您的代码,页面正在使用更新的数据刷新,但未显示警报。$scope.manifeststatus=数据后的3行;我认为延迟或$timeout不起作用,因为即使我延迟10秒。它会在我单击按钮后立即执行。我尝试了您的代码,页面正在使用更新的数据刷新,但未显示警报。$scope.manifeststatus=数据后的3行;我认为延迟或$timeout不起作用,因为即使我延迟10秒。它会在我单击按钮后立即执行。我尝试了您的代码,页面正在使用更新的数据刷新,但未显示警报。$scope.manifeststatus=数据后的3行;我认为延迟或$timeout不起作用,因为即使我延迟10秒。它会在我单击按钮后立即执行。我尝试了您的代码,页面正在使用更新的数据刷新,但未显示警报。$scope.manifeststatus=数据后的3行;我认为延迟或$timeout不起作用,因为即使我延迟10秒。我一点击按钮它就开始执行。