有人可以告诉我为什么在下面的代码中使用的setTimeout不起作用?它只是直接运行功能.
function change_txt_font(elem, id, text_fnt){
current_width = parseInt($('#span_text'+id).css('width'));
current_height = parseInt($('#span_text'+id).css('height'));
current_font_size = parseInt($("#span_text"+id).css("font-size"));
parent.document.getElementById(elem+'_f').value=text_fnt;
$('#span_text'+id).css('font-family',text_fnt);
$('#'+elem).css('font-family',text_fnt);
setTimeout(adjust_for_font(id),2000);
}
function adjust_for_font(id){
alert("function")
alert("id = "+id)
new_height = parseInt($('#span_text'+id).css('height'));
new_width = parseInt($('#span_text'+id).css('width'));
width_ratio = parseFloat(current_width/new_width)
height_ratio = parseFloat(current_height/new_height)
new_font_size = current_font_size * Math.min(width_ratio,height_ratio)
$("#text"+id).css("font-size", (parseFloat(new_font_size) - 1) + "px");
$("#span_text"+id).css("font-size", (parseFloat(new_font_size) - 1) + "px");
document.getElementById("form_front_text"+id).submit();
}document.getElementById("form_front_text"+id).submit();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏.
关于setTimeout工作方式,我有点困惑.我试图setTimeout在一个循环中,所以循环迭代,例如,相隔1秒.每个循环迭代发出一个HTTP请求,看起来另一端的服务器无法在如此短的时间内处理那么多请求.
for (var i = 1; i<=2000 && ok; i++) {
var options = {
host:'www.host.com',
path:'/path/'+i
};
setTimeout(makeRequest(options, i), 1000);
};
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用,我怎样才能做到这一点?
谢谢
我正在使用JavaScript中的promises并试图宣传setTimeout函数:
function timeout(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('timeout done');
}, ms);
});
}
var myPromise=timeout(3000);
myPromise.then(function(result) {
console.log(result); // timeout done
})
Run Code Online (Sandbox Code Playgroud)
相当简单,但我想知道如何在诺言结算之前取消我的超时.timeout返回Promise对象因此我松开了对setTimeout返回值的访问权限,并且无法通过取消超时clearTimeout.最好的方法是什么?
顺便说一句,这没有真正的目的,我只是想知道如何处理这个问题.我也把它放在这里http://plnkr.co/edit/NXFjs1dXWVFNEOeCV1BA?p=preview
我的问题是我无法停止计时器.
我有这个方法 来设置这个论坛的超时.它应该将标识符存储在全局变量中.我意外地发现,在我隐藏"mydiv"之后它仍在运行.
我现在还需要知道,如果递归函数创建多个实例,或者仅为超时创建一个实例.因为首先我认为它每次都会覆盖"var mytimer".现在我不太确定.
什么是一个可靠的方法来停止计时器?
var updatetimer= function () {
//do stuff
setTimeout(function (){updatetimer();}, 10000);
}//end function
//this should start and stop the timer
$("#mybutton").click(function(e) {
e.preventDefault();
if($('#mydiv').is(':visible')){
$('#mydiv').fadeOut('normal');
clearTimeout(updatetimer);
}else{
$('#mydiv').fadeIn('normal');
updatetimer();
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢,理查德
我尝试使用以下代码测试TCP连接.
System.Threading.Thread t = new System.Threading.Thread(() =>
{
using (TcpClient client = new TcpClient())
{
client.Connect(ip, Convert.ToInt32(port));
}
});
t.Start();
Run Code Online (Sandbox Code Playgroud)
如果IP或端口无效,如何设置超时?
我无法在initializers/devise.rb中使用设计会话超时来超时会话
我设置
config.timeout_in = 1.minute
Run Code Online (Sandbox Code Playgroud)
在initializers/devise.rb中,我与一个用户签约并保持闲置2分钟.这假设使当前会话无效并再次提示询问用户凭据但会话永远不会死亡.
我做错了什么或遗失了什么?
谢谢
假设我每隔10秒就需要从服务器获取一些数据.我将有一个函数通过AJAX获取数据,然后调用setTimeout再次调用此函数:
function GetData(){
$.ajax({
url: "data.json",
dataType: "json",
success: function(data){
// do somthing with the data
setTimeout(GetData, 10000);
},
error: function(){
setTimeout(GetData, 10000);
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果有人让网页全天开放,它可能会有数千个递归函数调用.
我不想使用setInterval,因为它没有考虑网络延迟.如果网络繁忙并且处理请求需要15秒,我不想在获得AJAX超时之前再次询问它.
处理需要定期调用的函数的最佳方法是什么?
Javascript的setTimeout功能是window对象的一种方法.此对象在ExtendScript中不存在,因此不适用于为Adobe InDesign或Illustrator制作的脚本.我可以使用什么来取得相同的结果呢?
我setTimeout在Node.js中使用它似乎与客户端的行为不同setTimeout,因为它返回一个对象而不是一个数字.我想将它存储在redis中,但由于redis只存储字符串,我需要将对象转换为字符串.但是,使用JSON.stringify抛出循环引用错误.如果我希望能够从redis中获取并调用它,如何将此对象存储在redis中clearTimeout?
我想在页面上分开线程,以防止gui冻结.为此,我运行的函数将使用setTimeout在另一个线程内冻结gui但仍然冻结.
代码和jsbin链接如下:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"
type="text/javascript"></script>
<meta charset="utf-8" />
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<input type="button" value="düðme" id="btn" />
<script type="text/javascript">
$("#btn").on("click",function(){
$("#div1").html(new Date());
});
$(document).ready(function(){
setTimeout(function() { count(); },1);
});
function count(){
for(var i =0;i<100000;i++){
$("#div2").html(i);
}
$("#div2").append(new Date());
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) settimeout ×10
javascript ×7
node.js ×2
.net ×1
adobe ×1
asynchronous ×1
c# ×1
cancellation ×1
connection ×1
devise ×1
extendscript ×1
jquery ×1
promise ×1
recursion ×1
redis ×1
tcp ×1