Как сказать, стрелял ли $ (окно) .load ()/window.onload событие уже?

У меня была похожая проблема, и она возникла ... HTH!

value = this.GetDate()

if (value.Length >= 6)//ensure that the date is mmddyy
{
    int year = 0;

    if (int.TryParse(value.Substring(4, 2), out year))
    {
        int pastMillenium = int.Parse(DateTime.Now.ToString("yyyy").Substring(0, 2)) - 1;

        if (year > int.Parse(DateTime.Now.ToString("yy")))//if its a future year it's most likely 19XX
        {
            value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium, year.ToString().PadLeft(2, '0'));
        }
        else
        {
            value = string.Format("{0}{1}{2}", value.Substring(0, 4), pastMillenium + 1, year.ToString().PadLeft(2, '0'));
        }
    }
    else
    {
        value = string.Empty;
    }
}
else
{
    value = string.Empty;
}
12
задан Lightness Races with Monica 21 August 2011 в 01:45
поделиться

5 ответов

You could try setting up a handler that's invoked via a timeout that will check the images to see if their properties are available. Clear the timer in the load event handler so if the load event occurs first, the timer won't fire. If the properties aren't available, then the load event hasn't fired yet and you know that your handler will eventually be invoked. If they are, then you know that the load event occurred before your handler was set and you can simply proceed.

Pseudocode

 var timer = null;
 $(function() {
    $(window).load( function() {
        if (timer) {
           clearTimeout(timer);
           timer = null;
        }
        process();
    });
    timer = setTimeout( function() {
        if (checkAvailable())
           process();
        }
    }, 10*1000 ); // waits 10 seconds before checking
 });

 function checkAvailable()
 {
     var available = true;
     $('img').each( function() {
         try {
             if (this.height == 0) {
                available = false;
                return false;
             }
         }
         catch (e) {
             available = false;
             return false;
         }
      });
      return available;
  }

  function process() {
      ... do the real work here
  }
7
ответ дан 2 December 2019 в 20:19
поделиться

Думаю, ваша проблема решится сама собой, если вы воспользуетесь $ (document) .ready вместо $ (window) .load - см. документацию jquery .

]
2
ответ дан 2 December 2019 в 20:19
поделиться

Не знаю, это то, что вам нужно, но пробовали ли вы (?):

$(document).ready(function(){
    ...
});

http://docs.jquery.com/Events/ready#fn

0
ответ дан 2 December 2019 в 20:19
поделиться

Я написал плагин, который может оказаться полезным: http://plugins.jquery.com/project/window-loaded

5
ответ дан 2 December 2019 в 20:19
поделиться

Ребята, вам стоит прочитать вот это:

http://web.enavu.com/daily-tip/daily-tip-difference-between-document-ready-and-window-load-in-jquery/

2
ответ дан 2 December 2019 в 20:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: