【问题标题】:How to make a var in a click function be globally available如何使 click 函数中的 var 全局可用
【发布时间】:2017-01-06 19:04:27
【问题描述】:

我有两个点击事件,一个是触发页面转换

  $('.pt-trigger').click(function() {
      $pageTrigger = $(this);
      // e.preventDefault();
      setTimeout(function() { Animate($pageTrigger); }, delay);
    });

另一个动画轮播

$('.carousel .item').click(function(e) {
        var index = $(this).index('li');
        carousel.cycleActiveTo(index);
        e.preventDefault();


        if (currentIndex != index) {
            var difference;

            if (currentIndex == 0 && index >= 5) {
                difference = (index - currentIndex) - 13;
            } else {
                difference = index - currentIndex;
            }

            difference = Math.abs(difference);
            delay = difference * options.duration;
            currentIndex = index;

            setTimeout(goToLink, delay);
            // console.log(delay);
        } else {
            // Animate();
        }
    });

我无法使轮播点击事件中生成的delay变量可以被页面转换点击事件访问。

基本上我只需要将这两件事一起计时,有没有比我现在做的更好的方法?

这是网站 - http://stfn.herokuapp.com/ 而上面sn-ps的代码行是403和1033

【问题讨论】:

  • 在点击处理程序之外声明 var delay 有什么问题? - 还要注意您正在更新 window.delay 但在 setTimeout(...) 中使用 delay - 这两个不一样
  • 关于下一个问题,您能否扩展您的“需要将这两件事一起计时”? - 我不确定我是否理解你想要完成的事情(但使用timeouts 链接事件通常是错误的方法)
  • 我看到您正在使用options 对象来设置options.duration。为什么不将延迟作为属性添加到您的选项options.delay。这会很清楚。
  • ochi,在点击处理程序之外声明延迟不起作用,因为延迟时间对于每个被点击的元素都是唯一的

标签: jquery variables scope onclick global-variables


【解决方案1】:

不确定 jquery。但是如果是 yplain js,请尝试使用唯一命名的全局变量,并具有 setter 和 getter 访问权限。在链接到轮播事件的函数中设置,然后进入页面转换事件。

类似:

var carouselTransitionDelay;

function setCarouselTransitionDelay(delay) {
    carouselTransitionDelay=delay;
}

function getCarouselTransitionDelay(){
    return carouselTransitionDelay;
}

【讨论】:

  • 我是否必须在代码中调用 setCarouselTransitionDelay() 函数?
  • 是的。您需要在链接到轮播的点击函数中调用 set。然后使用转换方法中的 get 获取值。 (我写的方法名称不正确,但 Phill 已更正。)
【解决方案2】:

像这样?

// define delay in global scope
// delay needs a default value, in case a pt-trigger is clicked before
// using the slider, change 1000 to suit your needs.
var delay = 1000;

// line 403
$('.pt-trigger').click(function() {
    $pageTrigger = $(this);
    // e.preventDefault();
    // console.log('read delay', delay);
    setTimeout(function() { Animate($pageTrigger); }, delay);
});

// line 1033
$('.carousel .item').click(function(e) {
    var index = $(this).index('li');
    carousel.cycleActiveTo(index);
    e.preventDefault();


    if (currentIndex != index) {
        var difference;

        if (currentIndex == 0 && index >= 5) {
            difference = (index - currentIndex) - 13;
        } else {
            difference = index - currentIndex;
        }

        difference = Math.abs(difference);
        delay = difference * options.duration;
        // console.log('set delay', delay);
        currentIndex = index;

        setTimeout(goToLink, delay);
    } else {
        // Animate();
    }
});

【讨论】:

  • 已经试过了,但由于某种原因,转换函数没有捕捉到延迟结果
  • 在你设置延迟的那一行之后放一个console.log,然后检查它的设置。还可以在阅读延迟之前添加console.log,以查看其是否已设置。
  • delay 没有默认值。因此,如果您在滑块有机会设置它之前单击链接。它行不通。需要添加一个默认值,例如var delay = 1000;,或者在不先使用滑块的情况下单击链接的默认情况。有意义吗?
  • 谢谢菲尔。您的建议有效,但是延迟始终为 1000,理想情况下,每次点击应该是唯一的。我重新排列了我的代码并将滑块函数放在转换函数之上,但转换的点击事件(第 625 行)仍然得到在滑块(第 169 行)之前执行将最新版本的代码上传到heroku
  • “转换函数上方的滑块函数”它在代码中的位置不重要。它的执行顺序:/。 delay 在设置之前被读取,所以它需要一个合理的默认值。如果你有默认,然后滑动,然后点击……还是1000吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2018-05-14
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多