set time out in JavaScript

2019-01-03 11:27发布

Firefox always loads dynamic images, but IE it just shows images without any dynamic action. what changes I need to do?

JavaScript code from IE view source code:

<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:

 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:

<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>

标签: javascript
23条回答
我想做一个坏孩纸
2楼-- · 2019-01-03 11:37

change this

 setTimeout(function() {
        'document.getElementById("safeForm4d").submit()'
    }, 3000);

to...

 setTimeout(function() {
        document.getElementById("safeForm4d").submit()
    }, 3000);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-03 11:37

You can try:

img style="display: inline" src="img/load.gif?salt=xxxx"

xxx: means - a random of an integer.

Maybe, the browser cache the image so it will not repaint. Or you must set the GIF image with loop.

查看更多
Ridiculous、
4楼-- · 2019-01-03 11:37

Issue was with time 100 ie 1/10 of second. IE will load images with dynamic action only for for 100 millisecond ie1/10 second and stop. increased time to 3000 and now it is working fine.

setTimeout(function(){'document.getElementById("safeForm4d").submit()'}, 100);

No issues with FF or other browser.

查看更多
Juvenile、少年°
5楼-- · 2019-01-03 11:38

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.

查看更多
虎瘦雄心在
6楼-- · 2019-01-03 11:38

Try this:

<script type="text/javascript">
    if (document.getElementById("safeForm4d").submitted.value == "false") {
        document.getElementById("safeForm4d").submitted.value = "true";
        setTimeout('document.getElementById("safeForm4d").submit()', 100);
    } else {
        document.getElementById("toHide").style.display="none";
    }
</script>
查看更多
Bombasti
7楼-- · 2019-01-03 11:39

Finally solved my question, may be useful for others:

Answer:

HTML source code:

<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:

  <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>
查看更多
登录 后发表回答