Variable scope issue with javascript function

后端 未结 4 620
滥情空心
滥情空心 2021-01-17 02:43

I am trying to create a function with spin.js. The function loads the spinner, then if it is called again with it argument, then it stops the spinner. I can\'t get the varia

4条回答
  •  悲&欢浪女
    2021-01-17 02:48

    Why don't you return the Spinner object from the function, and let the caller call .stop() on it when its time? Reads better, and it doesn't have to pollute the local scope with random variables, also makes the submitSpinner() simpler.

    function createSubmitSpinner() {
    
        $('body').append('
    '); return new Spinner({ lines: 13, length: 15, width: 5, radius: 20, color: '#ffffff', speed: 1, trail: 60, shadow: false }).spin(document.getElementById("spinner")); } $(function() { var spinner = createSubmitSpinner(); $('#overlay').on('click', function() { alert('click'); spinner.stop(); }); });

提交回复
热议问题