gpt4 book ai didi

javascript - 在输入显示 div 一段时间后

转载 作者:行者123 更新时间:2023-11-30 11:40:20 27 4
gpt4 key购买 nike

我有一个场景,当用户提供输入时我需要显示一个 div。

当用户连续更改输入值时,div 必须显示,当用户在 5 秒左右后停止提供输入时,div 必须隐藏。

控制键:

    function MainCtrl($scope,$timeout) {
$scope.appear = false;
$scope.showDiv = function() {
debugger;
$scope.appear = true;
}
$scope.$watch('appear',
function(newValue, oldValue) {
if(newValue && newValue===true){
$timeout(function(){
$scope.appear=false;
}, 5000);

}
}
);
}

html:

 <body ng-controller="MainCtrl">
Name: <input type="text" ng-change="showDiv()" ng-model="inputText"/> <br/>

<div ng-show="appear">Content</div>
</body>

DEMO

我在这里面临的问题是,当我在某个时间点连续提供输入时,div 会被隐藏并再次出现。

但我需要显示 div,直到提供输入,当用户在 5 秒后停止提供任何输入时,它应该消失。

如有任何帮助,我们将不胜感激。

最佳答案

我已经对您的 plunkr 进行了一些更改,请查看此链接 https://plnkr.co/edit/EsiG3Ifgk0maYF4GJTGm?p=preview

angular.module('plunker', []).controller('MainCtrl',function($scope,$timeout) {
$scope.appear = false;

$scope.timerId;
$scope.showDiv = function() {

if($scope.timerId!=undefined){
clearTimeout($scope.timerId);
}
$scope.appear = true;
$scope.timerId = $timeout(function(){
$scope.appear=false;
},5000);
}
});

关于javascript - 在输入显示 div 一段时间后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42919869/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com