javascript function is executed only one time - javascript

I have a javascript function and I want to call it 12 times.
So I did like this:
Here I have 12 images:
<img id="img1" src=""> </img>
<img id="img2" src=""> </img>
<img id="img3" src=""> </img>
<img id="img4" src=""> </img>
<img id="img5" src=""> </img>
<img id="img6" src=""> </img>
<img id="img7" src=""> </img>
<img id="img8" src=""> </img>
<img id="img9" src=""> </img>
<img id="img10" src=""> </img>
<img id="img11" src=""> </img>
<img id="img12" src=""> </img>
Here the function is defined:
function addImageSource(id,another_variable) {
var imageSource = "http://..."+ another_variable + "test";
$("#img" + id).attr("src", imageSource);
}
And here I call it:
var itm_id = 1;
while(itm_id < 13){
addImageSource(item_id, "another_variable" );
itm_id++
}
Why is this function executed only one time? Can somebody tell me why?

You'll need to use a for loop so you'll want something like
for (var itm_id = 1; itm_id < 13; itm_id++){
addImageSource(itm_id, "another_variable" );
}

You need for instead of if:
for (var itm_id = 1 ; itm_id < 13; itm_id++) {
addImageSource(itm_id, "another_variable" );
}
also you made a mistake with this line:
addImageSource(item_id, "another_variable" );
it should be:
addImageSource(itm_id, "another_variable" );

You need to place the function call inside of a loop. Right now you are using a conditional which is a decision making construct, not a loop.
Replace:
var itm_id = 1;
if (itm_id < 13){
addImageSource(item_id, "another_variable" );
itm_id++
}
With a for loop like this:
for (var itm_id=1 ; itm_id < 13 ; itm_id++) {
addImageSource(itm_id, "another_variable" );
}

Related

How to add HTML append javascript... "quotation marks" problems

I'm trying to transform div content from img to div strong
for example
<div class="multi-gallery-image show" id="service_preview">
<img alt="image" src="チャイルドカット¥3000">
<img alt="image" src="ジュニアカット">
<img alt="image" src="ハナコカット">
<img alt="image" src="Hair Styling">
<img alt="image" src="Manicures">
<img alt="image" src="Hair Coloring">
</div>
I want to transform to div strong
<div class="multi-gallery-image show" id="service_preview">
<div><strong>チャイルドカット¥3000</strong></div>
<div><strong>ジュニアカット</strong></div>
<div><strong>トップスタイリストカット</strong></div>
<div><strong>Hair Styling</strong></div>
<div><strong>Manicures</strong></div>
<div><strong>Hair Coloring</strong></div>
</div>
I have this but the result is different as expected:
let servicePreview = document.getElementById('service_preview'); //parent where I am trying to introduce the src values
let myImg;
let mySrc;
let toPush;
if (servicePreview.getElementsByTagName('img').length > 0){
let servicesNumber = servicePreview.getElementsByTagName('img').length; //number of img tags inside the service_preview
for (let i = 0; i < servicesNumber; i++){
myImg = servicePreview.getElementsByTagName('img')[i]; //capturing the img tag
mySrc = myImg.getAttribute('src'); //capturing the src value from img tag
toPush = '<div><strong>' + mySrc + '</strong></div>'; //creating html tag for push to the parent service_preview
servicePreview.append(toPush); //appending the html tag
}
}
but the result of this is
<div class="multi-gallery-image show" id="service_preview">
<img alt="image" src="チャイルドカット¥3000">
<img alt="image" src="ジュニアカット">
<img alt="image" src="ハナコカット">
<img alt="image" src="トップスタイリストカット">
<img alt="image" src="Hair Styling">
<img alt="image" src="Manicures">
"<div><strong>チャイルドカット¥3000</strong></div>"
"<div><strong>ジュニアカット</strong></div>"
"<div><strong>ハナコカット</strong></div>"
"<div><strong>トップスタイリストカット</strong></div>"
"<div><strong>Hair Styling</strong></div>"
"<div><strong>Manicures</strong></div>"
</div>
I want to delete that "quotes" on every div strong, that is a string.
I have to delete the complete img tag after solve the "quotes"
problem
use createElement to create dom element to appendChild and removeChild to remove elements.
let servicePreview = document.getElementById('service_preview');
var myImg;
var mySrc;
let toPush;
var elements = servicePreview.getElementsByTagName('img');
while (elements[0]) {
newDiv = document.createElement('div');
newStrong = document.createElement('strong');
newStrong.innerHTML = elements[0].getAttribute('src');
newDiv.appendChild(newStrong);
servicePreview.appendChild(newDiv);
elements[0].parentNode.removeChild(elements[0]);
}
<div class="multi-gallery-image show" id="service_preview">
<img alt="image" src="チャイルドカット¥3000">
<img alt="image" src="ジュニアカット">
<img alt="image" src="ハナコカット">
<img alt="image" src="Hair Styling">
<img alt="image" src="Manicures">
<img alt="image" src="Hair Coloring">
</div>
Unlike jQuery append() the native method treats html strings as text
Use insertAdjacentHTML(position, html) instead
const str = '<div class="inserted">Test</div>'
document.getElementById('one').append(str);// shows as text
document.getElementById('two').insertAdjacentHTML('beforeend', str)
.inserted{color:red}
<div id="one"></div>
<div id="two"></div>
In raw JavaScript I believe its something like this.
const services = ['チャイルドカット¥3000', 'ジュニアカット', 'ハナコカット',
'Hair Styling', 'Manicures', 'Hair Coloring']
const preview = document.getElementById("service_preview")
services.forEach(service =>{
const div = document.createElement("div")
const strong = document.createElement("strong")
strong.innerText = service
div.appendChild(strong)
preview.appendChild(div)
})
const servicePreview = document.getElementById("service_preview");
const imageSources = [...servicePreview.children].map((img) => img.src);
console.log(imageSources);
// Remove all images
while (servicePreview.firstChild) {
servicePreview.removeChild(servicePreview.firstChild);
}
// Add new div/strong tags
for (const imageSource of imageSources) {
const div = document.createElement("div");
const strong = document.createElement("strong");
strong.textContent = imageSource;
div.appendChild(strong);
servicePreview.appendChild(div);
}
simply use replaceChild() method
there is a trap with img.src because you get an URI
const servicePreview = document.getElementById('service_preview')
servicePreview.querySelectorAll('img').forEach(imgElm=>
{
let newDiv = document.createElement('div')
, newStrg = document.createElement('strong')
;
newDiv.appendChild( newStrg )
newStrg.textContent = imgElm.getAttribute('src') // or decodeURI( imgElm.src.split('/').pop() )
servicePreview.replaceChild( newDiv, imgElm )
})
<div class="multi-gallery-image show" id="service_preview">
<img alt="image" src="チャイルドカット¥3000">
<img alt="image" src="ジュニアカット">
<img alt="image" src="ハナコカット">
<img alt="image" src="Hair Styling">
<img alt="image" src="Manicures">
<img alt="image" src="Hair Coloring">
</div>

Automatic Cycling Carousel for several divs and different img

I am trying to write a function, that will automatically carousel cycle through img thumbs on my site with 12 different sets of images, which are just put together in one div. The javascript below works, but only as long, as I have the same amount of images in every div. I am also sure, that I took the long route of telling javascript, what to do in terms of variables so my question is, what should I change, so that I can have different amounts of img in my separate divs?
Thanks a lot for any tips!
var myIndex = 0;
carousel();
function carousel() {
var i;
var u = document.getElementsByClassName("thumbs1");
var v = document.getElementsByClassName("thumbs2");
var w = document.getElementsByClassName("thumbs3");
// and so on ...
for (i = 0; i < w.length; i++) {
u[i].style.display = "none";
v[i].style.display = "none";
w[i].style.display = "none";
// ...
}
myIndex++;
if (myIndex > w.length) {myIndex = 1}
u[myIndex-1].style.display = "inline-block";
v[myIndex-1].style.display = "inline-block";
w[myIndex-1].style.display = "inline-block";
// ...
setTimeout(carousel, 1200); // Change image every 2 seconds
}
<div class="imageholder">
<img class="thumbs1" src="image11.jpg">
<img class="thumbs1" src="image12.jpg">
<img class="thumbs1" src="image13.jpg">
<img class="thumbs1" src="image14.jpg">
</div>
<div class="imageholder">
<img class="thumbs2" src="image21.jpg">
<img class="thumbs2" src="image22.jpg">
<img class="thumbs2" src="image23.jpg">
</div>
<div class="imageholder">
<img class="thumbs3" src="image31.jpg">
<img class="thumbs3" src="image32.jpg">
<img class="thumbs3" src="image33.jpg">
<img class="thumbs3" src="image34.jpg">
<img class="thumbs3" src="image35.jpg">
</div>
<!-- ... -->
Keep counting the myIndex (so do not reset it) and use the modulus % operator with the array length of each image set.
Further notes:
The value of Timeout/Interval is in milliseconds. So 2000ms = 2s
You do not need to get all elements' references again with each call to carousel(). Just do it once at the beginning.
If something is not clear, please ask.
var myIndex = 0;
var i;
var u = document.getElementsByClassName("thumbs1");
var v = document.getElementsByClassName("thumbs2");
var w = document.getElementsByClassName("thumbs3");
var allThumbs = [u, v, w];
var myInterval = setInterval(carousel, 2000); // Change image every 2 seconds
function carousel() {
myIndex++;
for (i = 0; i < allThumbs.length; i++) {
allThumbs[i][(myIndex - 1) % allThumbs[i].length].style.display = "none";
allThumbs[i][myIndex % allThumbs[i].length].style.display = "inline-block";
}
}
.thumbs1:not(:first-child),
.thumbs2:not(:first-child),
.thumbs3:not(:first-child) {
display: none;
}
<div class="imageholder">
<img class="thumbs1" src="image11.jpg" alt="1">
<img class="thumbs1" src="image12.jpg" alt="2">
<img class="thumbs1" src="image13.jpg" alt="3">
<img class="thumbs1" src="image14.jpg" alt="4">
</div>
<div class="imageholder">
<img class="thumbs2" src="image21.jpg" alt="1">
<img class="thumbs2" src="image22.jpg" alt="2">
<img class="thumbs2" src="image23.jpg" alt="3">
</div>
<div class="imageholder">
<img class="thumbs3" src="image31.jpg" alt="1">
<img class="thumbs3" src="image32.jpg" alt="2">
<img class="thumbs3" src="image33.jpg" alt="3">
<img class="thumbs3" src="image34.jpg" alt="4">
<img class="thumbs3" src="image35.jpg" alt="5">
</div>

Javascript document.images.length returning 0

I am attempting to get all of the images with classname of tile into an array called tiles. I've tried a few things but it keeps giving me 0 length arrays/nodelists.
What am I doing wrong?
for(var i =0; i<document.images.length; i++){
var thumb = document.images[i]
if(thumb.className == "tile" && thumb.parentNode.tagName == "A")
tiles.push(thumb);
}
I have also tried
var allInputs = document.getElementsByTagName("img");
for(var i =0; i<allInputs.length; i++){
if(allInputs[i].className == "tile" tiles.push(allInputs[i]);
}
EDIT: Per request, here is all of the HTML code.
<body>
<form id="ct" action="">
<div id="head">
<img src="kgtitle.jpg" alt="Kiddergarden" />
</div>
<div id="menu">
<img src="kgmenu.jpg" alt="" />
</div>
<div id="title">
<img src="ctitle.jpg" alt="Matching Game" />
</div>
<div id="board">
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<br />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<br />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<br />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
</div>
<div id="main">
<p>Play the Concentration game! Click the tiles on the left and
match pairs of identical images.
<br /><br />
Click the <b>Reload Tiles</b>
button below to randomize the position of the tiles and play
again.
<br /><br />
Click the <b>Show Tiles</b> button to view the
solution.
</p>
</div>
<div id="controls">
<p>
<input type="button" value="Reload Tiles" id="reload" />
<input type="button" value="Show Tiles" id="showAll" />
</p>
</div>
<address>
Kiddergarden ·
A safe site on the Web for kids and families
</address>
This is the entirety of my javascript code
function addEvent(object, evName, fnName, cap) {
if (object.attachEvent)
object.attachEvent("on" + evName, fnName);
else if (object.addEventListener)
object.addEventListener(evName, fnName, cap);
}
function randomSort(arr) {
arr.sort(function () {
return 0.5 - Math.random();
});
}
function setOpacity(object, value) {
// Apply the opacity value for IE and non-IE browsers
object.style.filter = "alpha(opacity = " + value + ")";
object.style.opacity = value/100;
}
var flipCount = 0;
var firstFlip;
var secondFlip;
addEvent(window, "load", setupTiles(),false);
function setupTiles() {
var tiles = new Array();
alert(document.getElementsByTagName('img').length);
for(var i =0; i<document.getElementsByTagName("img").length; i++){
var thumb = document.getElementsByTagName("img");
thumb = thumb[i];
if(thumb.className == "tile" && thumb.parentNode.tagName == "A")
tiles.push(thumb);
}
var tileImages = new Array(tiles.length);
for(var j = 0; i < tileImages.length/2; j++){
tileImages[j] = new Image("tileimage"+j+".jpg");
}
for(var k = tileImages.length/2; i<tileImages.length;k++){
tileImages[k] = new Image("tileimage"+(i-tileImages.length)+".jpg");
}
randomSort(tileImages);
for(var l =0; i<tiles.length;l++){
tiles[l].image = tileImages[l];
tiles[l].onclick = flipTile;
}
/*document.getElementById("showAll").onclick = function () {
for(var i =0; i<tiles.length;i++){
tiles[i].src = tiles[i].image.src;
}
}
document.getElementById("reload").onclick = function () {
location.reload();
}*/
}
function flipTable(){
if(flipCount == 0){
this.src = this.image.src;
firstFlip = this;
flipCount++;
}
else if(flipCount == 1){
this.src = this.image.src;
secondFlip = this;
flipCount++;
checkTiles();
}
return false;
}
function checkTiles() {
if(firstFlip.image.src != secondFlip.image.src){
flipBack();
}
else{
flipCount=0;
firstFlip.opacity = 0.70;
firstFlip.style.filter = "alpha(opacity= 70)";
firstFlip.onclick = function () {
return false;
}
secondFlip.opacity = 0.70;
secondFlip.style.filter = "alpha(opacity= 70)";
secondFlip.onclick = function () {
return false;
}
}
}
function flipBack() {
firstFlip.src = "tile.jpg";
secondFlip.src = "tile.jpg";
flipCount = 0;
}
your script is fine, I just tested it. it probably returns 0 because you add the script to the top of you file, just make sure you add in the end:
<html>
<body>
<div id="board">
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
</div>
</body>
<script>
var tiles = [];
for(var i =0; i<document.images.length; i++){
var thumb = document.images[i]
if(thumb.className == "tile" && thumb.parentNode.tagName == "A")
tiles.push(thumb);
}
alert(tiles.length)
</script>
</html>
At least is the only cause I could find to return 0. Please let me know if it solved your issue.
If you ar importing the script from a different file, just add the import on the end, like this:
<html>
<body>
<div id="board">
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
<img src="tile.jpg" class="tile" alt="" />
</div>
</body>
<script src="myscript.js"></script>
</html>
a third option is to add your code in a function on document ready listener:
document.addEventListener("DOMContentLoaded", function(event) {
var tiles = [];
for(var i =0; i<document.images.length; i++){
var thumb = document.images[i]
if(thumb.className == "tile" && thumb.parentNode.tagName == "A")
tiles.push(thumb);
}
alert(tiles.length)
});

Changing all images on timer with JS

I have this 3x3 img gallery. I need all of them to change every certain time with JS.
So far I managed to do this to one of the images. I don't know how to target them all.
<script>
var images = ["http://lorempixel.com/250/200/", "http://lorempixel.com/250/150/"];
var i = 0;
var renew = setInterval(function(){
if(images.length == i){
i = 0;
}
else {
document.getElementByClassName('galleryItem').src = images[i];
i++;
}
},1000);
</script>
<div class="galleryWrapper">
<div class="galleryItem item1">
<img id="image1" src="http://lorempixel.com/250/200/" alt="picture1">
</div>
<div class="galleryItem item2">
<img id="image2" src="http://lorempixel.com/250/200/" alt="picture2">
</div>
<div class="galleryItem item3">
<img id="image3" src="http://lorempixel.com/250/200/" alt="picture3">
</div>
<div class="galleryItem item4">
<img id="image4" src="http://lorempixel.com/250/200/" alt="picture4">
</div>
<div class="galleryItem item5">
<img id="image5" src="http://lorempixel.com/250/200/" alt="picture5">
</div>
<div class="galleryItem item6">
<img id="image6" src="http://lorempixel.com/250/200/" alt="picture6">
</div>
<div class="galleryItem item7">
<img id="image7" src="http://lorempixel.com/250/200/" alt="picture7">
</div>
<div class="galleryItem item8">
<img id="image8" src="http://lorempixel.com/250/200/" alt="picture8">
</div>
<div class="galleryItem item9">
<img id="image9" src="http://lorempixel.com/250/200/" alt="picture9">
</div>
</div>
I don't know JS at all so don't be too harsh.
Thanks in advance.
Remove the onload from your body element (Using DOMContentLoaded event instead it is less intrusive).
var imageList = ["http://lorempixel.com/250/200/", "http://lorempixel.com/250/150/"],
imageListCounter = 0,
imageEls;
function swapImages() {
var i = 0, len = imageEls.length;
for (i = 0; i < len; i++) {
imageEls[i].src = imageList[imageListCounter];
}
imageListCounter++;
if (imageListCounter > imageList.length - 1) {
imageListCounter = 0;
}
}
document.addEventListener('DOMContentLoaded', function(event) {
imageEls = document.querySelectorAll('.galleryItem img');
setInterval(swapImages, 1000);
});
What we are doing here is:
on document ready store all the image elements in imageEls
then start a timer, calling swapImages every 1 second
swapImages iterates over images and changes the .src to what is in the next one in imageList
when the imageListCounter reaches the end of imageList it resets to 0
So the complete html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Gallery</title>
<link rel="stylesheet" href="style.css">
<script>
var imageList = ["http://lorempixel.com/250/200/", "http://lorempixel.com/250/150/"],
imageListCounter = 0,
imageEls;
function swapImages() {
var i = 0, len = imageEls.length;
for (i = 0; i < len; i++) {
imageEls[i].src = imageList[imageListCounter];
}
imageListCounter++;
if (imageListCounter > imageList.length - 1) {
imageListCounter = 0;
}
}
document.addEventListener('DOMContentLoaded', function(event) {
imageEls = document.querySelectorAll('.galleryItem img');
setInterval(swapImages, 1000);
});
</script>
</head>
<body>
<div class="galleryWrapper">
<div class="galleryItem item1">
<img src="http://lorempixel.com/250/200/" alt="picture1">
</div>
<div class="galleryItem item2">
<img src="http://lorempixel.com/250/200/" alt="picture2">
</div>
<div class="galleryItem item3">
<img src="http://lorempixel.com/250/200/" alt="picture3">
</div>
<div class="galleryItem item4">
<img src="http://lorempixel.com/250/200/" alt="picture4">
</div>
<div class="galleryItem item5">
<img src="http://lorempixel.com/250/200/" alt="picture5">
</div>
<div class="galleryItem item6">
<img src="http://lorempixel.com/250/200/" alt="picture6">
</div>
<div class="galleryItem item7">
<img src="http://lorempixel.com/250/200/" alt="picture7">
</div>
<div class="galleryItem item8">
<img src="http://lorempixel.com/250/200/" alt="picture8">
</div>
<div class="galleryItem item9">
<img src="http://lorempixel.com/250/200/" alt="picture9">
</div>
</div>
</body>
</html>
Where you set the image name to image1, you can just change to 'image'+i+''
document.getElementById('image'+i+'').src = images[i];
for (var e = 1; e < 10; e++)
{
var ImgID = 'image' + e;
document.getElementById(ImgID).src = images[i];
}
If you were using jquery you could just do a foreach on all the img elements. but the above will work. if you have 9 images with the ID of image1, image2, image3... you get the idea.
write this code in your else block
var imgs = document.getElementsByTagName('img');
for(j=0;j<imgs.length;j++){
imgs[j].src = images[i];
}
i++;
instead of this
document.getElementByClassName('gallery').src = images[i];
i++;
Give all your images a class attribute with the same value.
assuming you use the value "imagetags", replace your document.getElementById() line with the following:
var elements = document.GetElementsByClassName("imagetags");
for (var elem in elements){
elements[elem].src = images[i];
}

How to load images using web worker?

I am trying to load images using webworker api. I have large images in my html page its takes 5 mins to load all images therefore i am using webworker to load images.
here is technique..
I am keeping src attribute of all img tag empty in html page.
All images have unique id for each img e.g id = events_all_1_01 meaning image src will be "pictures/keywords/events/all/1/01.jpg". i.e last part events/all/1/01.jpg is id.
Main.html file
<body>
<script src="js/modernizr.custom.js"></script>
<script language="javascript">
window.onload = function(){
if (Modernizr.webworkers) {
var worker = new Worker('js/webworker/test_ww_23_04.js');
worker.onmessage = function(event) {
var url = event.data.replace(/_/g, "/");
var image_src = "pictures/keywords/"+url+".jpg";
var img = new Image();
img.src = image_src;
img.onload = function(){
// do stuff when your image is loaded
document.getElementById(event.data).src = image_src;
}
};
worker.onerror = function(e) {
alert('Error: Line ' + e.lineno + ' in ' + e.filename + ': ' + e.message);
};
var img_container = document.getElementById("wrapper");
var image_array = img_container.getElementsByTagName('img');
for(var i=0;i<image_array.length;i++){
var img_id = image_array[i].id;
console.log(img_id); // http://jsfiddle.net/5vyseob7/
postMessage(img_id); // http://jsfiddle.net/k04t6760/ here i am passing id one by one to webworker..
}
} // end of if condition
} // end of window.onload()
</script>
<div id="wrapper" style="height: 500px;width: 200px;overflow-y: auto;border: 1px solid gray;">
<div id="pictures1">
<div class="effect-1">
<div><img src="" id="events_all_1_01" width="150" height="100"></div>
<div><img src="" id="events_all_1_02" width="150" height="100"></div>
<div><img src="" id="events_all_1_03" width="150" height="100"></div>
<div><img src="" id="events_all_1_04" width="150" height="100"></div>
</div>
<hr/>
<div class="effect-2">
<div><img src="" id="events_all_2_01" width="150" height="100"></div>
<div><img src="" id="events_all_2_02" width="150" height="100"></div>
<div><img src="" id="events_all_2_03" width="150" height="100"></div>
<div><img src="" id="events_all_2_04" width="150" height="100"></div>
</div>
<hr/>
<div class="effect-3">
<div><img src="" id="events_all_3_01" width="150" height="100"></div>
<div><img src="" id="events_all_3_02" width="150" height="100"></div>
<div><img src="" id="events_all_3_03" width="150" height="100"></div>
<div><img src="" id="events_all_3_04" width="150" height="100"></div>
</div>
<hr/>
<div class="effect-4">
<div><img src="" id="events_all_4_01" width="150" height="100"></div>
<div><img src="" id="events_all_4_02" width="150" height="100"></div>
<div><img src="" id="events_all_4_03" width="150" height="100"></div>
<div><img src="" id="events_all_4_04" width="150" height="100"></div>
</div>
</div>
</div>
</body>
webworker code.
//var src = 'pictures/keywords/events/all/1/01.jpg';
//var id = src.substring(src.substring(0,18).length).split('.')[0].replace(/\//g, "_"); // Creating id here......events_all_1_01
//var fst = id.substring(0, id.lastIndexOf("_")+1); // get first part.... events_all_1
//var lst = parseInt(id.substr(id.lastIndexOf("_")+1)); // get last part i.e imagename ..01,02,03 etc....... and convert it to int
function LoadImages(currID) {
setTimeout(function() {
postMessage(currID);
}, 100);
}
self.onmessage = function(event) {
var currID = event.data;
LoadImages(currID);
};
I am getting following error :
Uncaught SyntaxError: Failed to execute 'postMessage' on 'Window': Invalid target origin '' in a call to 'postMessage'.
Typo error:
...
worker.onmessage = function(event) {
var url = e.data.replace(/_/g, "/");
e is not defined ... you probably meant function(e) or event.data.replace
UPDATE
Window don't have the postMessage method ... you need to use the worker method worker.postMessage

Categories