如何在 setTimeout 之后使用 addEventListener 调用函数?这是代码中的示例:
\n\nxyz = setTimeout(function () {\n [...blabla function...]\n // *(\xe2\x86\x93)\n if (window.addEventListener) {\n window.addEventListener(\'load\', blabla, false);\n } else {\n window.attachEvent(\'onload\', blabla);\n }\n}, 3000);\nRun Code Online (Sandbox Code Playgroud)\n\n*(\xe2\x86\x92):addEventListener在页面加载时调用该函数,但他应该在setTimeout为3秒后调用“blabla”函数。那么,如何解决这个问题呢?
\n我一直在没有窗口父级的情况下使用超时,如下所示:
setTimeout(FUNC, 1000);
Run Code Online (Sandbox Code Playgroud)
我很好奇,我应该像这样使用它吗?
window.setTimeout(FUNC, 1000);
Run Code Online (Sandbox Code Playgroud)
有什么区别吗?在没有窗户的情况下使用它时我没有注意到有什么不同。
我有一个数组,其中包含许多发送到外部应用程序的命令。
$scope.commandLog = [{"id":"1", "time":"12.02.2015 20:05:20.606","command":"cmd1", "status":"idle"},
{"id":"2", "time":"12.02.2015 20:05:20.606","command":"cmd2", "status":"idle"},
{"id":"3", "time":"12.02.2015 20:05:20.606","command":"cmd3", "status":"idle"},
{"id":"4", "time":"12.02.2015 20:05:44.162","command":"cmd4", "status":"idle"},
{"id":"5", "time":"12.02.2015 20:05:44.162","command":"cmd5", "status":"success"},
{"id":"6", "time":"12.02.2015 20:05:44.162","command":"cmd6", "status":"idle"},
{"id":"7", "time":"13.02.2015 12:05:52.181","command":"cmd7", "status":"idle"},
{"id":"8", "time":"13.02.2015 12:05:52.181","command":"cmd8", "status":"idle"}]
Run Code Online (Sandbox Code Playgroud)
我正在循环数组,并在下一个循环开始之前设置延迟。这工作正常。
for (var i = 0; i < $scope.commandLog.length; i++)
{
(function(index) {
setTimeout(function() {
// DO STUFF HERE
}, i * 2000);
})(i);
}
Run Code Online (Sandbox Code Playgroud)
我想更改当前数组索引处对象的状态,如下所示:
$scope.commandLog[index].status = 'active';
Run Code Online (Sandbox Code Playgroud)
我有一个显示命令及其状态的表格
<table class="table table-hover">
<thead>
<th>Time</th>
<th> </th>
<th>Command</th>
<th>Status</th>
</thead>
<tbody>
<tr ng-repeat="cmd in commandLog" ng-class="cmd.status" …Run Code Online (Sandbox Code Playgroud) 例如,在这个例子中我无法实现的东西是动态倒计时。
<span class="will-close">will be closed after : <strong>n</strong> seconds</span>
Run Code Online (Sandbox Code Playgroud)
正如你在上面看到的,我的时间很充裕,而且必须是倒计时
<span class="will-close">will be closed after : <strong>n</strong> seconds</span>
Run Code Online (Sandbox Code Playgroud)
$(function() {
setTimeout(function(e) {
$('#AniPopup').modal('show');
$(".will-close strong").html($("#AniPopup").attr("data-close"));
}, parseInt($('#AniPopup').attr('data-open')) * 1000);
setTimeout(function(e) {
$('#AniPopup').modal('hide');
}, parseInt($('#AniPopup').attr('data-close')) * 1000);
});Run Code Online (Sandbox Code Playgroud)
我最近声明要使用 vue.js,但我不明白为什么会使用 setTimeout() 发生这种行为。使用以下代码,无论 'time' 的值如何,setInterval(function(), time) 中定义的函数都会立即启动:
timerOn(){
...
this.avatar.timer.data = setTimeout( this.timerFunction(), 10000);
},
timerFunction(){
...
console.log('Hello!!');
clearTimeout(this.avatar.timer.data);
this.timerOn();
},
Run Code Online (Sandbox Code Playgroud)
但如果我使用以下代码,一切正常,并且 setInterval 内的函数在定义的“时间”之后发生:
timerOn(){
...
var This = this;
this.avatar.timer.data = setTimeout(function() { This.timerFunction()}, 10000);
},
timerFunction(){
...
console.log('Hello!!');
clearTimeout(this.avatar.timer.data);
this.timerOn();
},
Run Code Online (Sandbox Code Playgroud)
有人可以指导我并说为什么第一种方法失败了?
谢谢。
如果有更改,我的应用程序需要每 2 秒轮询一次。现在我使用setInterval,但有时myFunction执行时间会超过2秒,并且mysql数据库中的记录将被插入两次。我在 myFunction 中使用异步库..
所以我需要这个...
当 myFunction 完成时
method.myFunction() =>
它需要再次运行(但仅当它完成时)。setIntervalx 秒不是一个选项。这是因为如果脚本花费的时间超过 x 秒,它们可以同时运行。
我怎样才能myFunction在 x 秒后再次运行,但只有在myFunction处理完成时才能运行所以在伪代码中它会是这样
if method.myFunction == finished
wait 2 seconds
run method.myFunction again
我一直在尝试在 Angular 5 中创建一个简单的指令,但遇到了困难。我最近发现,将我的代码添加到setTimeout函数中可以使其按我的预期工作。在这种情况下,我希望表单显示为“ yolo”
我不完全明白为什么。我知道这与 Angular 引导的方式有关,但我不明白为什么会这样 - 特别是为什么构造函数中的代码被丢弃(毕竟,构造函数的意义何在?)
请在下面找到相关代码的简化副本:
@Directive({
selector: '[formControlName][phone]',
host: {
'(ngModelChange)': 'onInputChange($event)'
}
})
export class PhoneMask {
constructor(public model: NgControl) {
// with setTimeout
setTimeout(() => {
this.model.valueAccessor.writeValue('yolo');
});
// without setTimeout
// this.model.valueAccessor.writeValue('yolo');
}
}
@Component({
selector: 'my-app',
providers: [],
template: `
<form [formGroup]="form">
<input type="text" phone formControlName="phone">
</form>
`,
directives: [PhoneMask]
})
export class App {
constructor(fb:FormBuilder) {
this.form=fb.group({
phone:['']
})
}
}
@NgModule({
imports: [ …Run Code Online (Sandbox Code Playgroud) setTimeout 和 Web Worker 或多或少都做同样的事情。它们使主线程非阻塞,并在后台异步工作。我们如何判断何时使用什么?
我的应用程序中的搜索字段有自定义观察程序:
watch: {
search (query) {
if(query.length > 2) {
axios.post(url, query)
.then(res => {
console.log(res)
})
.catch(error => {
console.log(error)
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,search在我的例子中,我已经在 var 的每次更改值时向服务器发送了请求。我厌倦了将代码粘贴到里面setTimeout,但是当用户输入 3 次时,请求也发送了 3 次而不是一次。我需要在用户键入时等待,并在停止键入后向服务器发送一个请求。
setTimeout(function () {
// request code here
}, 3000);
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 vue.js 观察者中正确地做到这一点?
settimeout ×10
javascript ×9
asynchronous ×2
vue.js ×2
vuejs2 ×2
angular ×1
angularjs ×1
jquery ×1
node.js ×1
nonblocking ×1
nuxt.js ×1
setinterval ×1
web-worker ×1