我想在 likeMe() 循环完成后添加 5 秒的延迟。一旦完成,我想触发点击 Facebook 的注销按钮。
当谈到 javascript 语义时,我很愚蠢;因此我不知道如何触发这些?考虑到这一点,循环结束后我将在哪里/如何添加此效果:
我通常使用.promise().done()
在循环后执行finally步骤,但我不知道如何放置这个$.delay(5000)。在 JavaScript 中,promise().done( $('input[value="Log Out"]').click());
。
背景:使用 arudino、处理和 Greasemonkey jQuery 创建装置艺术作品。
完整脚本:
unsafeWindow.likeMe = likeMe;
function likeMe()
{
input = document.getElementsByTagName("input");
for(i = 0; i < input.length; i++)
{
myID = input[i].getAttribute("data-profileid");
if(myID != null && myID.indexOf("342584172457437") >= 0)
input[i].click();
}
$.delay(5000).promise().done( $('input[value="Log Out"]').click());
// this is wrong but yeah i have no clue where or how to put it on here.
}
$(window).load(function(){
var isCtrl = false;
document.onkeyup=function(e) {
if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 46 && isCtrl == true) {
likeMe()
return false;
}
}
});
最佳答案
您可以使用setTimeout
喜欢
function likeMe()
{
input = document.getElementsByTagName("input");
for(i = 0; i < input.length; i++)
{
myID = input[i].getAttribute("data-profileid");
if(myID != null && myID.indexOf("342584172457437") >= 0)
input[i].click();
}
setTimeout(function() {
$('input[value="Log Out"]').click();
}, 5000);
}
或在onkeydown
内,例如
document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 46 && isCtrl == true) {
likeMe();
setTimeout(function() {
$('input[value="Log Out"]').click();
}, 5000);
return false;
}
}
但不能同时在两者中。
关于javascript - 在 Javascript/jQuery 循环末尾添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11167162/