我有一个演示,其中用户在输入字段中键入任何内容,然后请求进入服务器。当前,只要用户键入它就会触发请求。 我只希望触发一个请求。例如,如果我输入class MyClass {
//Create an instance variable button1
var button1: SKLabelNode?
func runMathsProblem(){
//Your code removed for brevity
//This function now sets the instance variable rather than a local variable
button1 = SKLabelNode(fontNamed: "Bulky Pixels")
//Your code using Button1 removed for brevity
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.location(in: self)
//Now this code works.
if button1?.contains(pointOfTouch){
self.speed = 1
}
}
}
}
则触发三个请求。用户是否可以输入任何不间断的信息,停止一秒钟后,我会发出请求。
我知道可以使用ng-model-options指令对输入进行反跳操作:,但是它会在给定的时间后触发,但是我希望用户输入不停止的时间,但是会在停止后触发请求>
这是我的代码:
http://plnkr.co/edit/npiA2abAo5SEQFMMpKZO?p=preview
"abc"
答案 0 :(得分:1)
使用setTimeout
/ clearTimeout
来执行自己的反跳操作,如下所示:
app.controller('MainCtrl', function($scope,$http) {
$scope.name = 'World';
var timeout = null; // the timeout handle
$scope.keyupevt = function() {
clearTimeout(timeout); // clear any previous timeout (if null, nothing will happen)
timeout = setTimeout(function() { // set a new timeout
console.log('xx'); // that will make the request after 1 second
$http.get("data.json")
.then(function(response) {
console.log(response);
});
timeout = null; // reset timeout (not really necessary but it won't do any harm)
}, 1000); // the code inside will be called after exactly 1000 ms
}
});
每次按键按下时,都会将请求设置为在该按键按下事件发生1秒后发生。如果之前的请求尚未发生,则该请求将被取消,并且将设置一个新请求在1秒后发生。等等