0

Referring to this post I have this scenario

<html>
 <head>
  <script>
    // Our countdown plugin takes a callback, a duration, and an optional message
    $.fn.countdown = function (callback, duration, message) {
    // If no message is provided, we use an empty string
    message = message || "";
    // Get reference to container, and set initial content
    var container = $(this[0]).html(duration + message);
    // Get reference to the interval doing the countdown
    var countdown = setInterval(function () {
        // If seconds remain
        if (--duration) {
            // Update our container's message
            container.html(duration + message);
        // Otherwise
        } else {
            // Clear the countdown interval
            clearInterval(countdown);
            // And fire the callback passing our container as `this`
            callback.call(container);   
        }
    // Run interval every 1000ms (1 second)
    }, 1000);

    };
    // Function to be called after 5 seconds
     function redirect () {
     this.html("<a href='mypdf.pdf'>Donwload</a>");
     window.location = "http://msdn.microsoft.com";
     }
  </script>
 
 </head>
  <body>

Now, after "<boby>" tag I have an classic Asp response.write/flush like this:

<%
  ToLoop = 29
  for i = 0 to ToLoop
      StartTimer = Timer 'define time in second from 12:00AM

       if i = 1 then
         Count_Start_Second = round(This_Loop_Timer * ToLoop) 'use round for integer a decimal number
           response.write "<div id='countdown'></div>"
           response.write"<script>" & _
            "$('#countdown').countdown(redirect, " & Count_Start_Second & ", ""s remaining"");" & _
             "</script>"
           response.flush 
       end if

         --------More and more asp code to compile a dinamic PDF----------

        EndTimer = Timer 'define time in second after loop
        This_Loop_Timer = EndTimer - StartTimer 
  next
%>

I don't understand why the jQuery is executed after the page has finished loading? With the Flush, theorically, should load immediately after the first lap of the loop. But isn't.

halfer
  • 19,824
  • 17
  • 99
  • 186
Oscar Zarrus
  • 790
  • 1
  • 9
  • 17
  • I don't believe so. Since the connection hasn't closed yet, your DOM isn't loaded and Javascript won't run yet. You are referring to the $ jquery variable and it hasn't loaded yet. – Matt Oct 16 '13 at 17:20
  • Yes, sorry. I forgot to insert a code in my post. This: response.write "
    "
    – Oscar Zarrus Oct 16 '13 at 17:40
  • Since you are using a flush command and a timer in ASP, your page never finished loading, therefore none of your Jquery countdown code is running. That is why it runs after the page is finished, not during like your expecting. Also I'm assuming your even referencing the JQuery library ( I didn't see a script reference to it ) – Matt Oct 16 '13 at 17:47
  • yes there's a JQ library. The CountDown starts after, not during, but starts. But if I use another $ function like $('#someID').html('newText'), it works with flush and during loop (in other words, in real-time) – Oscar Zarrus Oct 16 '13 at 17:53
  • could you just flush ? – Matt Oct 16 '13 at 19:09
  • Re `` tag, do you mean ``? – halfer Jul 18 '21 at 12:17

0 Answers0