简体   繁体   English

如何从另一个功能停止功能,然后使用另一个参数重新启动

[英]How to stop function from another function then start it again with another parameter

i am trying to make a slider, i have function that getting array of objects and loop it, and i have select that change models list, but when i change select, old instance of function still work, they are starting to work together. 我正在尝试制作一个滑块,我具有获取对象数组并将其循环的功能,并且已经选择了更改模型列表,但是当我更改选择时,函数的旧实例仍然有效,它们开始协同工作。 So i add function stopper, but it wont work. 因此,我添加了功能塞,但无法正常工作。 Help please, thank you! 请帮助,谢谢!

       var keepGoing = true;

       function slideList(models) {
                    $.each(models, function (index, model) {
                        setTimeout(function () {
                            if (keepGoing != false) {
                        moditem.innerHTML = "";
                        modlist.value = model.id;
                        var photo = document.createElement('IMG');
                        photo.src = model.photos[0].file;
                        photo.classList.add('img');
                        moditem.appendChild(photo);
                        if (index >= models.length - 1) {
                            slideList(models);
                        }
                    } else {
                        return false;
                    }
                    },
                    5000 * index);
            });
        }

        function startLoop() {
            keepGoing = true;
        }

        function stopLoop() {
            keepGoing = false;
        }

        $("#car-type-select").on('change', function () {
            stopLoop();
            getModels(this.value);
        });

Clear timeout: 清除超时:

    var timer;
    function slideList(models) {
            $.each(models, function (index, model) {
                timer = setTimeout(function () {
                        moditem.innerHTML = "";
                        modlist.value = model.id;
                        var photo = document.createElement('IMG');
                        photo.src = model.photos[0].file;
                        photo.classList.add('img');
                        moditem.appendChild(photo);
                        if (index >= models.length - 1) {
                            slideList(models);
                        }
                    },
                    5000 * index);
            });
        }

        $("#car-type-select").on('change', function () {
            clearTimeout(timer);
            getModels(this.value);
        });

Other unimportant functions 其他不重要的功能

    function getModels(cartype_id) {
            $.ajax({
                type: 'POST',
                url: '/home/models/get',
                data: {
                    'cartype_id': cartype_id
                },
                success: function (models) {
                    makeList(models);
                    slideList(models)
                }
            });
        }

        function makeList(models) {
            modlist.innerHTML = "";
            $.each(models, function (index, model) {
                var option = document.createElement("option");
                option.text = model.manufacturer.name + ' ' + model.name;
                option.value = model.id;
                modlist.add(option);
            });
        }

You need to use clearTimeout to stop function in setTimout to stop executing. 您需要使用clearTimeout来停止setTimout函数以停止执行。

Example: 例:

var timer = setTimeout(function(){ /* code here */ }, 3000)

clearTimeout(timer) //it will stop setTimeout function from executing.

After looking at code update: 查看代码更新后:

Can you please try: 您可以尝试:

var timers = [];

function slideList(models) {
  $.each(models, function(index, model) {
    timers.push(setTimeout(function() {
        moditem.innerHTML = "";
        modlist.value = model.id;
        var photo = document.createElement('IMG');
        photo.src = model.photos[0].file;
        photo.classList.add('img');
        moditem.appendChild(photo);
        if (index >= models.length - 1) {
          slideList(models);
        }
      },
      5000 * index));
  });
}

$("#car-type-select").on('change', function() {
  $.each(timers, function(i, timer) {
    clearTimeout(timer);
  })
  getModels(this.value);
});

setTimeout is getting called in loop to need array of setTimeout references setTimeout在循环中被调用,需要setTimeout引用数组

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

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