在 JavaScript 中设置超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5651668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set time out in JavaScript
提问by bekur
Firefox always loads dynamic images, but IE it just shows images without any dynamic action. what changes I need to do?
Firefox 总是加载动态图片,但 IE 只显示图片,没有任何动态操作。我需要做什么改变?
JavaScript code from IE view source code:
JavaScript 代码来自 IE 查看源代码:
<script type=”text/javascript”
<!--/*--><![CDATA[/*><!--*/
if (document.getElementById("safeForm1d3").submitted.value == "false") {
document.getElementById("safeForm1d3").submitted.value = "true";
setTimeout('document.getElementById("safeForm1d3").submit()', 100);
}else{
document.getElementById("toHide").style.display="none";
}/*-->]]>*/
</script>
I am using Wicket framework, so real java code is:
我使用的是 Wicket 框架,所以真正的 Java 代码是:
static private class SafeSubmitBehaviour extends AbstractBehavior{
public void onRendered( Component component ) {
super.onRendered( component );
StringBuffer buffer = new StringBuffer(200);
buffer.append("<script type=\"text/javascript\" ><!--/*--><![CDATA[/*><!--*/\n");
buffer.append("if (document.getElementById(\"").append(component.getMarkupId()).append("\").submitted.value == \"false\") {\n");
buffer.append("document.getElementById(\"").append(component.getMarkupId()).append("\").submitted.value = \"true\";\n");
buffer.append("setTimeout('document.getElementById(\"").append(component.getMarkupId()).append("\").submit()', 100);\n}else{\n");
buffer.append("document.getElementById(\"toHide\").style.display=\"none\";\n}/*-->]]>*/</script>");
component.getResponse().write(buffer);
}
}
html page which loads my dynamic image is:
加载我的动态图像的 html 页面是:
<div id="toHide" class="pb-text-align-center">
<img style="display: inline" src="img/load.gif" />
<form wicket:id="safeForm" class="clearfix">
<input type="hidden" wicket:id="submitted" value="false" />
</form>
</div>
采纳答案by bekur
solved my problem. may be useful for others:
解决了我的问题。可能对其他人有用:
Answer:
回答:
HTML source code:
HTML源代码:
<SCRIPT type="text/javascript">
var $ = jQuery.noConflict();
document.getElementById('toHide').style.display ="";
$('#toHide').doTimeout(1000, function() {
$('#toHide').find('#safeForm34').submit();
document.getElementById('myAnimatedImage').src = "../../img/load.gif";
});
</SCRIPT>
html:
html:
<div id="toHide" class="pb-text-align-center">
<img src="img/load.gif" id='myAnimatedImage' style="margin-left: auto; margin-right: auto;"/>
<form wicket:id="safeForm" class="clearfix" />
</div>
回答by Aron Rotteveel
Because setTimeout()
requires your function to be passed as a string or as an anonymous function:
因为setTimeout()
要求您的函数作为字符串或匿名函数传递:
setTimeout(function() { document.getElementById("safeFormec").submit(); }, 100);
回答by ADW
Take away the quotes from around your action:
从你的行动中拿走引号:
setTimeout(function() {
document.getElementById("safeForm4d").submit();
}, 3000);
回答by jeroenjoosen
Try something like this
尝试这样的事情
setTimeout(function(){document.getElementById("safeForm9c").submit();}, 100);
In the old days a setTimeout complete function was in a string format, but these days we use it this way. Also this way makes is possible to do more things when the timeout is complete.
在过去, setTimeout 完整函数采用字符串格式,但现在我们以这种方式使用它。这种方式也可以在超时完成时做更多的事情。
回答by Andre Backlund
Try wrapping them inside a function:
尝试将它们包装在一个函数中:
setTimeout(function(){ document.getElementById("safeForm4d").submit(); }, 100);
回答by Paul D. Waite
Have you tried removing the function call brackets from the end of this line?
您是否尝试从这一行的末尾删除函数调用括号?
document.getElementById("safeForm9c").submit()
i.e. do this instead:
即改为这样做:
setTimeout(document.getElementById("safeForm9c").submit, 100)
You're telling IE to call the results of submit()
in 100 milliseconds time, rather than call submit.
您告诉 IEsubmit()
在 100 毫秒内调用结果,而不是调用提交。
回答by Neeraj
Can you try doing something like
你能尝试做类似的事情吗
setTimeout('document.getElementById("safeForm9c").submit()', 100);
Probably setTimeout accepts the things you want to call as a string, so that it can do an eval after the timeout and run the script as it encouters in the string.
可能 setTimeout 接受您想要作为字符串调用的内容,以便它可以在超时后执行 eval 并在字符串中遇到脚本时运行脚本。
回答by mplungjan
Include this in the head
将其包含在头部
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
and have
并且有
static private class SafeSubmitBehaviour extends AbstractBehavior{
public void onRendered( Component component ) {
super.onRendered( component );
StringBuffer buffer = new StringBuffer(200);
buffer.append("<script type=\"text/javascript\" >\n");
buffer.append("var input = $(\"input[name='submitted']\");\n");
buffer.append("if (input.val() == \"false\") {\n");
buffer.append(" input.val(\"true\");\n");
buffer.append(" setTimeout(function(){ $(\"#").append(component.getMarkupId()).append("\").submit()}, 100);\n");
buffer.append("}\n");
buffer.append("else {\n $(\"#toHide\").hide();\n}");
component.getResponse().write(buffer);
}
}
which should render
哪个应该渲染
var input = $("input[name='submitted']");
if (input.val() == "false") {
input.val("true");
setTimeout(function(){ $("#safeForm1d3").submit()}, 100);
}else{
$("#toHide").hide();
}
Where would you do the $("#toHide").show();
?
你会在哪里做$("#toHide").show();
?
回答by Vivek
change this
改变这个
setTimeout(function() {
'document.getElementById("safeForm4d").submit()'
}, 3000);
to...
到...
setTimeout(function() {
document.getElementById("safeForm4d").submit()
}, 3000);
回答by Vivek
function setsubmit()
{
document.getElementById("safeFormec").submit();
}
setTimeout('setsubmit()',100);