奇怪的是,我指的是这两件事:
如果有一种绕过#2的方式 - 这将是非常棒的,但是现在我只是好奇这一点.
简短的片段来解释我在说什么:
for (var i=0; i<10; i++) setTimeout("addInput('.')",i*500);
setTimeout('addInput("</br>")',5100);
Run Code Online (Sandbox Code Playgroud)
在上面,除非addInput('.')被引号括起,否则忽略延迟并且只执行代码; 除非我向第二行添加超时,否则它将在第一个超时完成之前执行.
我有一个用户定义的函数,我想延迟它的执行.想要延迟fancy(),我正在使用setTimeout.但它瞬间运行.我也设置了不同的时间延迟,但它没有影响.是否有任何其他方法或我使用它错了?请帮忙.
提前致谢.阿里
$('a.vid').click(function(){
setTimeout(fancy(this) ,2000 );
});
function fancy(that){
var videoFile = $(that).attr('videofile');
var videoWidth = Number($(that).attr('videowidth'));
var videoHeight =Number( $(that).attr('videoheight'));
var videoCode = '<video width="'+videoWidth+'" height="'+videoHeight+'" controls autoplay autobuffer><source src="includes/video/'+videoFile+'.ogv" type="video/ogg" /><source src="includes/video/'+videoFile+'.mp4" type="video/mp4" /><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+videoWidth+'" height="'+(videoHeight+40)+'" id="lynda_video_player" align="middle"><param name="allowScriptAccess" value="sameDomain"><param name="allowFullScreen" value="true"><param name="movie" value="lynda_video_player.swf?videoFile=includes/video/'+videoFile+'.mp4&skinFile=lynda_video_skin.swf&videoFileWidth='+videoWidth+'&videoFileHeight='+videoHeight+'"><param name="quality" value="high"><param name="wmode" value="transparent"><param name="scale" value="noscale"><param name="salign" value="lt"><embed src="lynda_video_player.swf?videoFile=includes/video/'+videoFile+'.mp4&skinFile=lynda_video_skin.swf&videoFileWidth='+videoWidth+'&videoFileHeight='+videoHeight+'" quality="high" width="'+videoWidth+'" height="'+(videoHeight+40)+'" name="lynda_video_player" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" scale="noscale" salign="lt" wmode="transparent" allowfullscreen="true" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></video>';
$('#videoPlayer').html(videoCode);
$.fancybox({
'transitionIn' : 'fade',
'transitionOut' : 'fade',
'overlayColor' : '#000', …Run Code Online (Sandbox Code Playgroud) var a = function(bool){
(function b(bool){
if(bool === true || bool === undefined){
console.log('running');
setTimeout(b,50);
}else{
clearTimeout(b);
}
})();
}
Run Code Online (Sandbox Code Playgroud)
我已经知道这可能比setInterval清除此功能更好.我已经尝试过休息和返回,并且它一直在循环console.log
有关如何轻松取消此功能的任何建议?
我试过的
if(bool === true || bool === undefined){
console.log('running');
setTimeout(b,50);
}else{
return;
}
var d;
if(bool === true || bool === undefined){
console.log('running');
d=setTimeout(b,50);
}else{
clearTimeout(d);
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个简单的游戏.我试图在几秒钟后递归复制一个div.重复后,它会使用新的唯一ID(ID + i)创建新的div.
这个想法是它不断创建div,用户必须点击它们才能在它达到最大值(游戏结束)之前尽可能地删除它们.
它不会正确等待创建div.我想每隔几秒就从现有的div中创建新的div,但是一旦我运行它就创建全部15或者它只创建1并停在那里.
JSFIDDLE - https://jsfiddle.net/namelesshonor/msrkxq63/
function spawnFly() {
if(x >= 15){
alert("YOU LOST\n15 Flys have infested your screen!");
}
else if(x < 15) {
x++; // adds another fly to the counter
setTimeout(duplicate(), 2000); // spawns a new fly after a few secs
animateDiv(); // animate the spawned fly
spawnFly(); // called recursively until fly count is met
}
};
function duplicate() {
var original = document.getElementById('fly'+i);
var clone = original.cloneNode(true);
clone.id = "fly" + i++;
clone.onclick …Run Code Online (Sandbox Code Playgroud) 在NodeJS中是否有可能实现该版本setTimeout,而不会在最后一行代码完成后阻止进程退出?
也就是说setTimeout,只有在进程仍在运行的情况下,才会触发回调函数的条件类型。
实际示例:
当实现通过设置一些超时来初始化自身的库时,您希望应用程序完成后就无需对该库进行显式调用以清除所有超时,并且无论如何都要让应用程序关闭。
我在javascript中制作一个简单的游戏.这些是我的玩家方法(有问题的部分是在玩家击中水后半秒钟调用重置方法):
Player.prototype = {
update : function(){
// player control and edge of screen detection
// TODO factor out hardcoding into sprite width constants
if (this.keyPressed === 'right' && this.x < 400){
this.x += 100;
}
if (this.keyPressed === 'left' && this.x > 10){
this.x += -100;
}
if (this.keyPressed === 'up' && this.y > 10){
this.y -= 83;
}
if (this.keyPressed === 'down' && this.y < 400){
this.y -= -83;
}
// reset key press
this.keyPressed = null;
// …Run Code Online (Sandbox Code Playgroud) 我想等待循环的每次迭代,除了第一次.这是我的代码,在setTimeout函数等待一些x秒后立即运行所有迭代而没有等待,第一次迭代正常工作.
这是代码
var requests_made = 0;
drivers.forEach(function(driver) {
if (requests_made == 0) {
createUser(data);
} else {
setTimeout(function () {
createUser(data);
},30000);
}
requests_made++;
});
Run Code Online (Sandbox Code Playgroud) 当Ajax调用成功时,用户将被重定向到主页面:
$('#button_1').on('click', function () {
//code
$.ajax({
method: "POST",
url: "/somewhere",
data: {
//code
}
}).done(function () {
location.href = "/";
});
});
Run Code Online (Sandbox Code Playgroud)
重定向后,我想自动点击另一个按钮,打开一个模态.我试过里面的done函数不同的方法:
.done(function () {
location.href = "/";
$('#button_2').click(); // doesn't work
});
.done(function () {
location.href = "/";
setTimeout(function ()
$('#button_2').click(); // doesn't execute
}, 2000);
});
.done(function () {
location.href = "/";
$(document).ready( function () {
// executes but user is immediately redirected to main page
$('#button_2').click();
});
});
Run Code Online (Sandbox Code Playgroud)
我也尝试在Ajax调用所在的函数中使用Ajax,但结果相同.
在Ajax调用之后,如何以编程方式单击按钮?
this.setState(prevState => ({
score: prevState.score + 10,
rightAnswers: prevState.rightAnswers + 1,
currentQuestion: setTimeout(() => {
prevState.currentQuestion + 1
}, 2000)
}))
}
Run Code Online (Sandbox Code Playgroud)
在按钮上单击我更改状态。我的目标是延迟currentQuestion状态更改,在此期间我想显示某些状态消息,但我想立即更新分数而没有延迟。
这样做的正确方法是什么?
PS:此变体无法正常工作,它只代表我想做的事情。
谢谢。
我正在测试Promise对象并编写了一些代码,这些代码模拟了一个同步的长时间运行的任务.我正在比较Promise和setTimeout - 请参阅小提琴:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Promise vs setTimeout</h2>
<div><button id="settimeout-test">setTimeout with slow running function</button></div>
<div><button id="promise-test">Promise and slow running function</button></div>
<div><button id="clear">Clear Results</button></div>
<h5>Results</h5>
<div id="result"></div>
<script>
const slow = function() {
let nu = Date.now();
while (Date.now() - nu < 1000) {}
}
const getSlowPromise = () => new Promise(resolve => {
slow();
resolve();
});
const resultsElement = document.getElementById('result')
const log = (message) => {
resultsElement.innerText += message;
}
const …Run Code Online (Sandbox Code Playgroud) settimeout ×10
javascript ×9
jquery ×2
node.js ×2
asynchronous ×1
delay ×1
es6-promise ×1
fancybox ×1
function ×1
html ×1
promise ×1
prototype ×1
reactjs ×1
setstate ×1
sleep ×1
this ×1
timedelay ×1