Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将setTimeout()添加到查询选择器_Javascript - Fatal编程技术网

Javascript 如何将setTimeout()添加到查询选择器

Javascript 如何将setTimeout()添加到查询选择器,javascript,Javascript,我有一个JS脚本 <script> document.querySelectorAll('.stylebutton').forEach(function(e) { e.addEventListener('click', function() { this.style.backgroundColor = 'black'; }) }); </script> document.querySelectorAll('.stylebutton').forEac

我有一个JS脚本

<script>
  document.querySelectorAll('.stylebutton').forEach(function(e) {
  e.addEventListener('click', function() {
    this.style.backgroundColor = 'black';
  })
});
</script>

document.querySelectorAll('.stylebutton').forEach(函数(e){
e、 addEventListener('单击',函数()){
this.style.backgroundColor='black';
})
});
我想在跑步前等10秒钟。我试过了

<script>
  document.querySelectorAll('.stylebutton').forEach(function(e) {
  e.addEventListener('click', setTimeout(function() {
    this.style.backgroundColor = 'black';
  }),10000)
});
</script>

document.querySelectorAll('.stylebutton').forEach(函数(e){
e、 addEventListener('click',setTimeout(函数(){
this.style.backgroundColor='black';
}),10000)
});

但是我得到一个控制台错误
不能定义这个
setTimeout
应该在回调内部调用

document.querySelectorAll('.stylebutton').forEach(function(e) {
  e.addEventListener('click', function() {
    setTimeout(()=>this.style.backgroundColor = 'black', 10000);
  })
});

setTimeout
应在回调内部调用

document.querySelectorAll('.stylebutton').forEach(function(e) {
  e.addEventListener('click', function() {
    setTimeout(()=>this.style.backgroundColor = 'black', 10000);
  })
});

在函数()中尝试=>setTiimeout….在运行附加事件侦听器的代码之前要等待10秒,或者在单击后运行每个事件侦听器的代码之前要等待10秒?在函数()中尝试=>setTiimeout….您想在运行附加事件侦听器的代码之前等待10秒,还是想在单击后在运行每个事件侦听器的代码之前等待10秒?太好了,谢谢!我不知道它必须在外面callback@hedgethenight很高兴能帮忙。太好了,谢谢!我不知道它必须在外面callback@hedgethenight很乐意帮忙。