简体   繁体   English

如何正确重用功能?

[英]How can I reuse a function properly?

I try to make 3 basic slideshow. 我尝试制作3个基本幻灯片。

I made this code for the first one and wanted to use it on the other 2 as well with the New slideS() method and with some parameter changing.But it's not working,even the first function is'nt working if I put parameter in it. 我为第一个编写了此代码,并希望通过New slideS()方法和一些参数更改在其他两个代码上使用它。但是它不起作用,即使我将参数放入第一个函数也不起作用。它。

Can somebody explain me why is this not working and how to fix it?Thanks beforehand! 有人可以向我解释为什么这不起作用以及如何解决它吗?谢谢!

var img = document.getElementById("asd");
var imgArr = ["1.jpg", "3.png", "3.png"];
var i = 0;

function slideS(a) {

  a.src = imgArr[i];

  if (i < imgArr.length - 1) {

    i++;

  } else {

    i = 0;
  }
  setTimeout("slideS()", 1500);
}
slideS(img)

You could do something like this, using an object oriented approach: 您可以使用面向对象的方法执行以下操作:

function SlideShow(el, imagesArray, msDelay) {
  this.el = el;
  this.images = imagesArray;
  this.delay = (msDelay) ? msDelay : 1000;
  this.timer = null;

  this.Run = function () {
    var self = this;
    var index = 0;
    this.timer = setInterval(function(){
      self.el.src = self.images[index++ % self.images.length];
    }, this.delay);
  }

  this.Stop = function() {
    this.timer = null;
  }
}

var img = document.getElementById("asd");
var imgArr = ["1.jpg", "3.png", "3.png"];
var delay = 1500;
var ss = new SlideShow(img, imgArr, delay);
ss.Run();
...
ss.Stop();

Would that work for you? 那对你有用吗? Then you are using pure functions and an object that can be used to start, stop, and manage any slide show. 然后,您将使用纯函数和可用于开始,停止和管理任何幻灯片放映的对象。

You could use a closure over the element and the array and use setInterval instead of setTimeout . 您可以对元素和数组使用闭包,并使用setInterval代替setTimeout

 function slide(id, array) { function swap() { image.src = array[i]; i++; i %= array.length; } var image = document.getElementById(id), i = 0; setInterval(swap, 1500); } slide('image1', ['http://lorempixel.com/400/200/', 'http://lorempixel.com/400/200/', 'http://lorempixel.com/400/200/']); 
 <image id="image1"></image> 

I think you want like: 我想你想像:

Remove setTimeout. 删除setTimeout。 And use setInterval: 并使用setInterval:

setInterval(function(){
  slideS(img)
},1500)

try this... you are making mistake at some places 试试这个...你在某些地方犯了错误

var img = document.getElementById("asd");
var imgArr = ["1.jpg", "3.png", "3.png"];
var i = 0;

function slideS(a) {

  a.src = imgArr[i];

  if (i < imgArr.length - 1) {

    i++;

  } else {

    i = 0;
  }

  setTimeout(() => slideS(a), 1500);
/* you need to pass function to setTimeout and pass refrence of image that's 'a' */

// or use function instead of arrow function

  setTimeout(function() { slides(a) }, 1500);

}
slideS(img)

hope this helps.. 希望这可以帮助..

I assume it works when the function doesn't take a parameter? 我假设函数不带参数时可以使用吗?

Then, the reason it would work with no parameter, but stop working with a parameter, is that the setTimeout tries to recursively call the function but doesn't pass a parameter. 然后,它不使用任何参数但停止使用参数的原因是setTimeout尝试递归调用该函数但不传递参数。 So you'd change that to 所以你将其更改为

setTimeout(() => {slideS(a);}, 1500);

But then when you try to run multiple instances of this concurrently, you'll get into trouble because your'e using global variables. 但是,当您尝试同时运行多个实例时,就会遇到麻烦,因为您正在使用全局变量。 You'll need to use something more local (perhaps closures?) for your lcv, for example. 例如,您需要为LCV使用更本地的内容(可能是闭包?)。

You have to use setInterval instead of setTimeout 您必须使用setInterval而不是setTimeout

 var img = document.getElementById("asd"); var imgArr = ["https://i.stack.imgur.com/lgt0W.png", "https://i.stack.imgur.com/X0fKm.png", "https://i.stack.imgur.com/YfPSD.png"]; var i = 0; function slideS(a) { a.src = imgArr[i]; if (i < imgArr.length - 1) { i++; } else { i = 0; } } slideS(img); //initial call to start it without having to wait for 1500 ms to pass setInterval(function() { slideS(img); }, 1500); 
 <img id="asd"> 

一种 乙 C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM