I have seen many questions concerning randomly selecting array items without repeating. However, most of them are answered by using the splice method. But this removes items.
I have already selected my items randomly, but they are repeating. In my two functions I select from each randomly selected item two "sub-items". The two functions are not working together, I am looking for a way that it is possible to select two different randomly selected items, without repetition, and without removing them. Can one please help me out?
(Using Adobe Edge Animate)
var xml_source = "series.xml";
var initLoadScript = false;
var items = [];
var itemTitle1;
var obj = new Object();
var previousNumber = -1;
loadXML();
function loadXML() {
$.ajax({
type: "GET",
url: xml_source,
dataType: "xml",
success: function(xml) {
$(xml).find('sbs').find('channel').find('item').each(function() {
items.push($(this));
});
itemOne();
itemTwo();
}
});
}
function itemOne(){
var randomNumber = Math.floor(Math.random()*14);
var assignItem = randomNumber;
console.log("random nummer 1: " + assignItem);
sym.$("TitleText1").html(items[assignItem].find("author_name").text());
sym.$("Image1").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>");
}
function itemTwo(){
var randomNumber = Math.floor(Math.random()*14);
var assignItem = randomNumber;
console.log("random nummer 2: " + assignItem);
sym.$("TitleText2").html(items[assignItem].find("author_name").text());
sym.$("Image2").html("<img src='"+items[assignItem].find('media\\:content, content').find('media\\:thumbnail, thumbnail').attr('url')+"' width='145'/>");
}
Example of XML Structure:
<?xml version="1.0" encoding="UTF-8" ?>
<sbs version="1.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Feed</title>
<description>Video</description>
<link>//www.URL.com</link>
<lastBuildDate>Fri</lastBuildDate>
<pubDate>Fri</pubDate>
<ttl>1</ttl>
<item>
<title>title</title>
<description>aflevering</description>
<link>//www.google.com</link>
<guid>//www.google.com</guid>
<formatname>berg</formatname>
<pubDate>Wed</pubDate>
<!-- oEmbed -->
<oembed>
<version>1.0</version>
<type>video</type>
<provider_name>provider</provider_name>
<provider_url>http://www.video.nl/url</provider_url>
<width>10</width>
<height>10</height>
<title>title1</title>
<author_name>author name</author_name>
<author_url>http://www.google.com/</author_url>
</oembed>
<media:content
url="http://google.com"
type="text/html"
medium="document"
expression="full"
height="10"
width="10"
lang="us">
<media:title type="plain">title</media:title>
<media:description type="plain">title descr</media:description>
<media:thumbnail url="http://google.com/tiger.jpg" width="10" height="10" />
</media:content>
</item>
Try
jsfiddle http://jsfiddle.net/guest271314/o5tfs48r/
I don't get very well what you're trying to achieve, but here's one way I'd get random items once
EDIT: Improved performance due to @JLRishe comment
If you want to go through an array in random order without modifying the original array:
ary.slice()
(this will copy the array, but not its values if they are objects).