0

有人可以帮我在这段代码中输入 javascript 延迟。我想让这段代码在 2 秒后打开。当您打开网址时。网站的其余部分正常加载。

<script type="text/javascript">

var avail=$z:value[article.availableinstock];

if ($z:value[article.availableinstock] < 1)
{
document.write('<div class="shop_not">');
document.write("In order!");
}
else if ($z:value[article.availableinstock] >=100)
{
document.write('<div class="shop_ok">');
document.write(" 100+ in stock" );
}
else if ($z:value[article.availableinstock] >=50 )
{
document.write('<div class="shop_ok">');
document.write(" 50+ in stock" );
}
else if ($z:value[article.availableinstock] >=25 )
{
document.write('<div class="shop_ok">');
document.write(" 25+ in stock" );
}
else
{
document.write('<div class="shop_bob">');
document.write(+ avail.toFixed(0));
document.write(" in stock" );
}
</script>
4

2 回答 2

1

解决方案是将所有内容都放在一个setTimeout(function() {... your stuff ...}, 2000)调用中。

这应该看起来像:

function yourStuff() {
  var avail=$z:value[article.availableinstock];

  if ($z:value[article.availableinstock] < 1) {
    document.write('<div class="shop_not">');
    document.write("In order!");
  } else if ($z:value[article.availableinstock] >=100) {
    document.write('<div class="shop_ok">');
    document.write(" 100+ in stock" );
  } else if ($z:value[article.availableinstock] >=50 ) {
    document.write('<div class="shop_ok">');
    document.write(" 50+ in stock" );
  } else if ($z:value[article.availableinstock] >=25 ) {
    document.write('<div class="shop_ok">');
    document.write(" 25+ in stock" );
  } else {
    document.write('<div class="shop_bob">');
    document.write(+ avail.toFixed(0));
    document.write(" in stock" );
  }
}

然后,在页面内的某个地方,body您只需调用

<script type="text/javascript">setTimeout(yourStuff, 2000);</script>
于 2012-10-12T09:33:12.703 回答
0

将您的代码包含在一个函数中并使用setTimeoutJS 函数。

function function_name () {
    var avail=$z:value[article.availableinstock];

    if ($z:value[article.availableinstock] < 1)
    {
        document.write('<div class="shop_not">');
        document.write("In order!");
    }
    else if ($z:value[article.availableinstock] >=100)
    {
        document.write('<div class="shop_ok">');
        document.write(" 100+ in stock" );
    }
    else if ($z:value[article.availableinstock] >=50 )
    {
        document.write('<div class="shop_ok">');
        document.write(" 50+ in stock" );
    }
    else if ($z:value[article.availableinstock] >=25 )
    {
        document.write('<div class="shop_ok">');
        document.write(" 25+ in stock" );
    }
    else
    {
        document.write('<div class="shop_bob">');
        document.write(+ avail.toFixed(0));
        document.write(" in stock" );
    }
}

setTimeout('function_name()', 2000);
于 2012-10-12T09:33:11.237 回答