A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: detect average swf play time

  1. #1
    Senior Kid
    Join Date
    Jan 2002
    Location
    I am right behind ya
    Posts
    274

    detect average swf play time

    Hi,
    i'd like to detect the time of my swf being viewed before gets closed, or get to another page or the browser window gets closed. how do i achieve that? i dont have the access to the onClose() function. thanks in advance.
    -The Dancin kidd

  2. #2
    Junior Member
    Join Date
    Feb 2006
    Posts
    4
    in action's panel of frame one of main timelines
    Code:
    getURL("javascript:document.body.onunload=function(){alert('swf played for: '+(playtime/60000).toFixed(2)+' minutes.');};this.update=function(a){playtime=a;};history.go(-1);");
    var start=getTimer();
    setInterval(function(){getURL("javascript:update("+(getTimer()-start)+")")},1000);

  3. #3
    Senior Kid
    Join Date
    Jan 2002
    Location
    I am right behind ya
    Posts
    274
    thanks =D
    -The Dancin kidd

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    -nt

  5. #5
    Senior Kid
    Join Date
    Jan 2002
    Location
    I am right behind ya
    Posts
    274
    =( it doesnt work when user closes the window. without warning, there is not enuff time to access the database to update the counter. any other clue =(
    -The Dancin kidd

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    call the javascript function once immediately, and reduce the interval. now its polling only once per second, and doesnt start for one second.
    Code:
    getURL("javascript:document.body.onunload=function(){alert('swf played for: '+(playtime/60000).toFixed(2)+' minutes.');};this.update=function(a){playtime=a;};history.go(-1);");
    var start=getTimer();
    function update(){getURL("javascript:update("+(getTimer()-start)+")");}
    update();
    setInterval(update,1000);

  7. #7
    Senior Kid
    Join Date
    Jan 2002
    Location
    I am right behind ya
    Posts
    274
    huh? but when window is closed, javascript lost its working environment and wont continue anymore. remove the alert box and try it yourself.

    thanks though
    -The Dancin kidd

  8. #8
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    the onunload fires before the window closes - javascript doesnt lose it's 'environment' in any case - the script will stop all script op execution until each statement is resolved. change the alert to a prompt('',''); followed by a window.open('http://www.flashkit.com'','',''), run the script, get the prompt, leave for 3 days, come back, answer the prompt and that window will still open. and yes, well, taking the alert out would definitely mean i wouldnt see the alert. not sure what you're talking about with the database - i dont see any mention of anything besides wanting to tell the user how long he was there - thus the alert. however, assuming you want to post the results serverside ? and maybe open that page ? instead of alerting - this works
    Code:
    getURL("javascript:window.onunload=function(){window.open('http://www.yoursite.com/howlong.txt','','');};history.go(-1);");
    var loader=new LoadVars();
    var start=getTimer();
    function update(){loader.playtime=getTimer()-start;loader.sendAndLoad("http://www.yoursite.com/howlong.php",loader,"POST");}
    setInterval(update,500);
    and howlong.php would be
    Code:
    <?php fwrite(fopen("howlong.txt","w+"),"SWF played for ".$playtime/60000." minutes."); ?>

  9. #9
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    hmph wasnt sure this would work - it does
    Code:
    getURL("javascript:window.onunload=function(){window.open(  'http://www.yoursite.com/howlong.txt','','');};history.go(-1);");
    var loader = new LoadVars();
    var start = getTimer();
    function update(){
    	loader.playtime = getTimer()-start;
    	loader.sendAndLoad("http://www.yoursite.com/howlong.php", loader, "POST");
    }
    loader.onLoad=update;
    update();

  10. #10
    Senior Kid
    Join Date
    Jan 2002
    Location
    I am right behind ya
    Posts
    274
    that would generate a popup window, it is a disturbance to the user experience =(
    -The Dancin kidd

  11. #11
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    then don't open the window... it's only included as a demonstration - take that out and the results are still saved to the data file, display it however you want.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center