使此Javascript函数仅在第一次匹配时运行

use*_*292 0 javascript jquery

我试图让这个函数只在第一场比赛中添加var!(或运行一次)

  if ($.isFunction($.fn.flexslider)) {

        $("div.gallery-to-slideshow").each(function (index, item) {
            var gallery = new Gallery($(item));
        });
Run Code Online (Sandbox Code Playgroud)

我尝试添加:首先在不同的地方,但我是Javascript的新手,仍然在努力理解它!

Jac*_*ack 7

你可以使用first() http://api.jquery.com/first/

    if ($.isFunction($.fn.flexslider)) {
       var gallery = new Gallery($("div.gallery-to-slideshow").first());
     }
Run Code Online (Sandbox Code Playgroud)