在运行时JavaScript中创建匿名函数

时间:2015-11-27 19:24:25

标签: javascript jquery

我有下一个代码结构:

每页的global.js和* .js。让我们以global.js和main_menu.js为例。

我有一些元素,有类ripple_effect,当点击这个类的元素时,动画开始运行650ms,所以为了让用户看到动画我尝试在代码中做一个小延迟(我知道这个不好,但仍然。)

在global.js中的

我有下一个代码:

var delayForRipple = 300;

function Execute(func) {
    setTimeout(func(), delayForRipple);
}
在main_menu.js中的

我有下一个代码:

$(this).on('click', ".menu_button", function (e) {
        href = $(this).attr('id');
        Execute(function () {
            window.location.href = href;
        });
    });

但是此代码会立即执行。我该如何解决?

这是快速复制的小提琴:



var delayForRipple = 300;

$(".wrapper").on('click', "#clickMe", function (e) {
    Execute(function () {
        alert("TEST");
    });
});

function Execute(func) {
    setTimeout(func(), delayForRipple);
}

.test_element {
    height:100px;
    width:100px;
    background-color:red;
}

<div class="wrapper">
    <div id="clickMe" class="test_element"></div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

你做错了......你必须将回调传递给setTimeout才能执行而不是执行它你自己

执行类似

的操作
topics

甚至删除冗余的执行功能

function Execute(func) {
   setTimeout(func, delayForRipple);
}

答案 1 :(得分:2)

这很有效。

function Execute(func) {
    setTimeout(func, 2000);   
}

Execute(function() {
   alert("test"); 
});

答案 2 :(得分:1)

func

中将setTimeout更改为app.controller('categoryController', function ($http, $scope, $location, $rootScope, $routeParams, $anchorScroll) { loaderShow(); $scope.filtered = {}; $scope.minp = 0; $scope.maxp = 0; $scope.pdts = {}; $http.get(domain + "/get-category-products/" + $routeParams.url_key + (window.localStorage.getItem('id') != null ? "?userId=" + window.localStorage.getItem('id') : ""), { cache: true }).success(function (data, status, headers, config) { $scope.products = data; $scope.pdts = data.data $scope.filters = data.filters; $scope.$digest; loaderHide(); }); $scope.load = function (event, url) { angular.element(event.target).children("i").addClass("fa fa-spinner fa-pulse"); $http.get(url, { params: { 'filters': $scope.filtered, 'minp': $scope.minp, 'maxp': $scope.maxp, 'slug': $routeParams.url_key, 'sort': jQuery("select.orderby").val(), 'userId': (window.localStorage.getItem('id') != null ? window.localStorage.getItem('id') : "") }, cache: true }).success(function (data, status, headers, config) { $scope.products = data; if (data.data.length > 0) { jQuery.each(data.data, function (k, v) { $scope.pdts.push(v); }); angular.element(event.target).children("i").removeClass("fa fa-spinner fa-pulse"); } else { angular.element(event.target).removeAttr("ng-click"); angular.element(event.target).text("No More Products"); } $scope.$digest; loaderHide(); }); }; $scope.filterProds = function (option, parent) { if (option) { if (!(parent in $scope.filtered)) $scope.filtered[parent] = []; var idx = $scope.filtered[parent].indexOf(option); if (idx > -1) $scope.filtered[parent].splice(idx, 1); else $scope.filtered[parent].push(option); if ($scope.filtered[parent].length <= 0) delete $scope.filtered[parent]; } }; $scope.applyFilters = function () { $scope.minp = jQuery("#min_price").val(); $scope.maxp = jQuery("#max_price").val(); $http.get(domain + "/get-filtered-products", { params: { 'filters': $scope.filtered, 'minp': $scope.minp, 'maxp': $scope.maxp, 'slug': $routeParams.url_key, 'sort': jQuery("select.orderby").val(), 'userId': (window.localStorage.getItem('id') != null ? window.localStorage.getItem('id') : "") } }).success(function (response) { $scope.products = response; $scope.pdts = response.data $scope.$digest; jQuery(".big-notification.yellow-notification").slideUp(); }); } $scope.sizeOf = function (obj) { return Object.keys(obj).length; }; $scope.showFilters = function () { jQuery(".big-notification.yellow-notification").toggle("slideDown"); } $scope.showOptions = function (e) { jQuery("#" + e).toggle(); } $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) { siteMainFn(); }); });