追加不同的随机数为URL在JavaScript数组在每个循环 [英] Appending Different Random Number To URL In Javascript Array On Each Loop

查看:118
本文介绍了追加不同的随机数为URL在JavaScript数组在每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图(但没有成功),创建一个包含幻灯片加载到一个iframe的数组。其中一个帧的(/ Events.php)的使用PHP来查询一个Word preSS数据库并显示1后随机选择。这张幻灯片需要显示的不同的随机后的每次数组遍历。

I'm trying (without much success) to create an array which contains slides being loaded into an iframe. One of these frames (/Events.php) uses PHP to query a WordPress database and show 1 post chosen at random. This slide needs to show a different random post every time the array loops through.

在他们我的code一刻...

My code at them moment is...

<script type="text/javascript">

var frames = Array(
    'http://www.example.com/Slide01.php', 5,
    'http://www.example.com/Slide02.php', 5,
    getRandomUrl(), 5,
    'http://www.example.com/Slide04.php', 5
    );

var i = 0, len = frames.length;

function getRandomUrl()
{
    return "http://www.example.com/Events.php?=" + (new Date().getTime());
}

function ChangeSrc()
{
  if (i >= len) { i = 0; } // start over
  document.getElementById('myiframe').src = frames[i++];
  setTimeout('ChangeSrc()', (frames[i++]*1000));
}
window.onload = ChangeSrc;
</script>

唯一的麻烦是,每次的 /Events.php 的显示它已追加到它相同的号码,以便因此显示每个回路同一职位。

The only trouble is everytime /Events.php is shown it has the same number appended to it so therefore shows the same post in each loop.

我要追加一个不同的号码给的 /Events.php 的每个循环,因此产生不同的内容,每次幻灯片。

I need to append a different number to the /Events.php slide on each loop so it generates different content each time.

我开始觉得我这样的方向是正确的任何帮助或指针接近这完全错误的方式将AP preciated!

I'm starting to think I'm approaching this in totally the wrong way so any help or pointers in the right direction would be appreciated!

干杯,
马克。

Cheers, Mark.

推荐答案

的问题是,你只叫 getRandomUrl()一旦这是您在定义的数组,这装置的值将始终是相同的作为其唯一返回一次。

The issue is you are only calling getRandomUrl() once which is when you defined your array, this means the value will always be the same as its only returned once.

一个解决办法是对函数本身的存储阵列中,像这样:

One solution would be to store the function itself in your array like so:

var frames = Array(
    'http://www.example.com/Slide01.php', 5,
    'http://www.example.com/Slide02.php', 5,
    getRandomUrl, 5,
    'http://www.example.com/Slide04.php', 5
    );

然后调用它的 ChangeSrc()如果一个函数

function ChangeSrc()
{
  if (i >= len) { i = 0; } // start over
    var frame = frames[i++],
        isFnc = typeof(frame) == "function";
    if(isFnc){
        frame = frame();
    }
    document.getElementById('myiframe').src = frame;
    setTimeout(function(){
        ChangeSrc()
    }, frames[i++]*1000);
}

http://jsfiddle.net/redgg6pq/

这篇关于追加不同的随机数为URL在JavaScript数组在每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆