www.webdeveloper.com
Results 1 to 8 of 8

Thread: Load image during executing a javascript

  1. #1
    Join Date
    Aug 2011
    Posts
    6

    Load image during executing a javascript

    Newbie here, please be kind.

    Want to reload a new image during executing a javascript. How can I do this?
    Attached below is my trial script (problem portion only). It's intention is to
    display 111.jpg for 2 seconds, followed by 222.jpg and 333.jpg. The actual
    behavior is that it displays blank.jpg for six seconds then displays 333.jpg.

    I think that the
    <IMG src="111.jpg" ,,,,,,,
    does not take place during wait loop, as script is running.

    Can any guru here help me how I can resolve this?
    AdvThanksance
    Tak

    ----- attachment -----
    <html><head>
    <script type="text/javascript">
    function picStart() {
    var a = new Array(3);
    a[1]="111.JPG";
    a[2]="222.JPG";
    a[3]="333.JPG";
    for(i=1;i<4;i++) {
    document.getElementById("mainpic").src=a[i];
    var time=new Date(); t=time.getTime();
    do { var time=new Date();tNew=time.getTime();
    // alert(tNew+" "+t);
    }while(tNew-t < 2000);
    }
    }
    </script>
    </head>
    <body>
    <A href="javascripticStart()">Click Start</a><br>
    <IMG id="mainpic" src="blank.jpg" width="800" height="800">
    </body>
    </html>
    --------- end attachment ------

  2. #2
    Join Date
    May 2011
    Posts
    194

    preload and render

    Hi,

    run javascript f.e. load or reload an image dynamically

    <script type="text/javascript">

    // +++++ changeable code
    var arPicFileName = new Array
    (
    "111.JPG",
    "222.JPG",
    "333.JPG"
    );


    var arPicWith = new Array
    (
    111,
    222,
    333
    );

    var arPicHeight = new Array
    (
    101,
    202,
    303
    );

    var intWaitingTimeAfterEveryChangeOfSrc=2000; // Milliseconds, what you want, > 0


    // ++++ internal code, do not change

    var intLengthOfAllPicArrays=arPicFileName.length; // must be > 0

    var intCounter=0;

    var arobjPreloadedPics=new Array(); // length is intLengthOfAllPicArrays

    var bofuChangePicLoopAllowed=false;
    var intfuChangePicLoopIndex=-1;
    var intfuChangePicLoopTimeoutID=0;

    var objHtmlImg;

    // ----- preload pic data during loading js-file
    for(intCounter=0;intCounter<intLengthOfAllPicArrays;intCounter++)
    {
    arobjPreloadedPics[arobjPreloadedPics]=new Image(arPicWith[intCounter],arPicHeight[intCounter]);
    arobjPreloadedPics[arobjPreloadedPics].src=arPicFileName[intCounter]; // preload, string to .src
    }


    // ----- change pic
    function fuChangePicLoop() // need objHtmlImg, endless loop till stop
    // no args, all vars are global (loop should not have local vars)
    {
    if(bofuChangePicLoopAllowed) // false --> see fuStopChangePicLoop()
    {
    // next index and check
    intfuChangePicLoopIndex++;
    if(intfuChangePicLoopIndex>=intLengthOfAllPicArrays){intfuChangePicLoopIndex=0;}

    // change objHtmlImg.src
    objHtmlImg.src=arobjPreloadedPics[intfuChangePicLoopIndex].src; // preload now renders

    // wait and then loop
    intfuChangePicLoopTimeoutID=windows.setTimeout('fuChangePicLoop()',intWaitingTimeAfterEveryChangeOfS rc);
    }
    }

    // ----- stop change
    function fuStopChangePicLoop() // onclick too
    {
    // stop loop of fuChangePicLoop()
    bofuChangePicLoopAllowed=false;

    // clear timer of fuChangePicLoop()
    if(intfuChangePicLoopTimeoutID!=0)
    {window.clearTimeout(intfuChangePicLoopTimeoutID);}

    // init
    intfuChangePicLoopTimeoutID=0; // must be 0
    intfuChangePicLoopIndex=-1; // must be -1
    }

    // ----- start change
    function fuStartChangePicLoop(stID) // only onclick
    // stID string of html element with property .src
    {
    fuStopChangePicLoop(); // first stop

    // check ID and get html image object
    if(stID!=null)
    {if(stID!="")
    {objHtmlImg=document.getElementById(stID) // global
    if(objHtmlImg!=null)
    {
    bofuChangePicLoopAllowed=true;

    fuChangePicLoop(); // need objHtmlImg, endless loop till stop
    }
    }
    }
    }


    </script>
    </head>
    <body>
    <a href="javascript:fuStartChangePicLoop("mainpic");">Start change of image (endless loop till stop clicked)</a><br>
    <a href="javascript:fuStopChangePicLoop();">Stop change of image</a><br>

    <IMG id="mainpic">
    </body>
    </html>

  3. #3
    Join Date
    Aug 2011
    Posts
    6
    Thanks twseitex,

    Wow, there are many techniques that I do not know. Will dig in.
    There seems some errors in your code, however.

    Have changed:
    <a href="javascript:fuStartChangePicLoop("mainpic");">
    to
    <a href='javascript:fuStartChangePicLoop("mainpic");'>
    .
    But it still does not work. fuStartChangePicLoop(stID) has an unknown error,
    and the wait loop has a syntax error (do not know yet).
    Will try to look into them.

    Thanks anyway.
    Oh, by the way, what does "javascript f.e." mean? Sorry for naive question.
    Tak

  4. #4
    Join Date
    Aug 2011
    Posts
    6
    OK,
    I understand all of your code. Debugged, and it is now working.
    The key tecnique is setTimeout("itselt", instead of do/while loop.
    Many thanks for the help.
    Tak

    p.s. I still have some concern about self calling function, because
    it may cause stack overflow. No problem?

  5. #5
    Join Date
    May 2011
    Posts
    194

    sorry for errors

    Hi,

    here runable code and see zip file, have fun :-)


    <html>
    <head>
    <script type="text/javascript">

    // cat Tom like a gif animation

    // +++++ changeable code
    var arPicFileName = new Array
    (
    "00.jpg",
    "01.jpg",
    "02.jpg",
    "03.jpg",
    "04.jpg",
    "05.jpg",
    "06.jpg",
    "07.jpg",
    "08.jpg",
    "09.jpg"
    );

    var arPicWith = new Array
    (
    130,
    130,
    130,
    130,
    130,
    130,
    130,
    130,
    130
    );

    var arPicHeight = new Array
    (
    190,
    190,
    190,
    190,
    190,
    190,
    190,
    190,
    190
    );

    var intWaitingTimeAfterEveryChangeOfSrc=100; // Milliseconds, what you want, > 0


    // ++++ internal code, do not change

    var intLengthOfAllPicArrays=arPicFileName.length; // must be > 0

    var intCounter=0;

    var arobjPreloadedPics=new Array(); // length is intLengthOfAllPicArrays

    var bofuChangePicLoopAllowed=false;
    var intfuChangePicLoopIndex=-1;
    var intfuChangePicLoopTimeoutID=0;

    var objHtmlImg;

    // ----- preload pic data during loading js-file
    for(intCounter=0;intCounter<intLengthOfAllPicArrays;intCounter++)
    {
    arobjPreloadedPics[intCounter]=new Image(arPicWith[intCounter],arPicHeight[intCounter]);
    arobjPreloadedPics[intCounter].src=arPicFileName[intCounter]; // preload, string to .src
    }


    // ----- change pic
    function fuChangePicLoop() // need objHtmlImg, endless loop till stop
    // no args, all vars are global (loop should not have local vars)
    {
    if(bofuChangePicLoopAllowed) // false --> see fuStopChangePicLoop()
    {
    // next index and check
    intfuChangePicLoopIndex++;
    if(intfuChangePicLoopIndex>=intLengthOfAllPicArrays){intfuChangePicLoopIndex=0;}

    // change objHtmlImg.src
    objHtmlImg.src=arobjPreloadedPics[intfuChangePicLoopIndex].src; // preload now renders

    // wait and then loop
    intfuChangePicLoopTimeoutID=window.setTimeout('fuChangePicLoop()',intWaitingTimeAfterEveryChangeOfSr c);
    }
    }

    // ----- stop change
    function fuStopChangePicLoop() // onclick too
    {
    // stop loop of fuChangePicLoop()
    bofuChangePicLoopAllowed=false;

    // clear timer of fuChangePicLoop()
    if(intfuChangePicLoopTimeoutID!=0)
    {window.clearTimeout(intfuChangePicLoopTimeoutID);}

    // init
    intfuChangePicLoopTimeoutID=0; // must be 0
    intfuChangePicLoopIndex=-1; // must be -1
    }

    // ----- start change
    function fuStartChangePicLoop(stID) // only onclick
    // stID string of html element with property .src
    {
    fuStopChangePicLoop(); // first stop

    // check ID and get html image object
    if(stID!=null)
    {if(stID!="")
    {objHtmlImg=document.getElementById(stID) // global
    if(objHtmlImg!=null)
    {
    bofuChangePicLoopAllowed=true;

    fuChangePicLoop(); // need objHtmlImg, endless loop till stop
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <H1>Cat Tom like a gif animation</H1>
    <a href="#" onclick="fuStartChangePicLoop('mainpic');">Start running of Tom (endless loop till stop clicked)</a><br>
    <a href="#" onclick="fuStopChangePicLoop();">Stop running of Tom</a><br>

    <IMG id="mainpic" src="1x1.gif" alt"Cat Tom"> <!-- 1 pixel transparent -->
    </body>
    </html>

  6. #6
    Join Date
    Aug 2011
    Posts
    6
    Great,,, charming!

    One problem still remain, that I could not resolve yet. This may be a javascript
    limitation.

    [Problem:]
    Say, the original jpg width is 1000 pix, and wanted display width is 100pix.
    The "arobjPreloadedPics[0]=new Image(100," does not force the display width to 100,
    and displays 1000 instead.
    How can I force shrink the display?

    Tak

  7. #7
    Join Date
    May 2011
    Posts
    194

    image.width or image.style.width

    Hi,

    f.e.

    <IMG ID="idofImage" SRC="1x1pic_transparent.gif>

    <script>

    var real_width=100;
    var real_height=200;

    var scale_width=50;
    var scale_height=100;


    // preload
    var objImage=new Image(real_width,real_height);
    objImage.src="real_pic";

    ....

    // render with new dimension
    document.getelementById("idofImage").src=objImage.src;

    document.getelementById("idofImage").width=scale_width;
    document.getelementById("idofImage").height=scale_height;

    // same document.getelementById("idofImage").style.width and document.getelementById("idofImage").style.height
    </script>


    Internet Explorer and style.zoom see http://msdn.microsoft.com/en-us/library/ms535169.aspx

  8. #8
    Join Date
    Aug 2011
    Posts
    6
    Thanks, twseitex.

    Very much appreciated!
    And, thanks for the pointer to msdn.
    Tak

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.19389 seconds
  • Memory Usage 2,929KB
  • Queries Executed 15 (?)
More Information
Template Usage (32):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates