当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Javascript function fires on pageload instead of onclick

问题描述:

<script type="text/javascript">

var tId;

$(function() {

$('#btnSign').click(function() {

InitiatePolling();

});

});

function InitiatePolling() {

event.preventDefault();

tId = setInterval(Poll, 1000);

}

function Poll() {

event.preventDefault();

var res = <%= PollResponse() %>;

}

</script>

I am trying to write a javascript function that fires on a click event from the front end. and calls a method in the backend. the method can indeed be called, the problem here is that the function Poll() executes upon the loading of this user control. how do I prevent the function to run prematurely?

edit:

$(document).ready(function() {

$('#btnSign').click(function() {

tId = setInterval(function() {Poll();}, 1000);

 function Poll(){

var res = <%= PollResponse() %>;

}

});

});

I moved everything inside the functiuon that should fire when the button is clicked, but the line with the PollResponse() call is executed every postback anyway. maybe this is the wrong way to go about this problem, is there a safer way to call back end methods within a user control javascript?

网友答案:

Try to subscribe to click event like this:

$("body").on("click", "#btnSign", function() {
    InitiatePolling();
});
网友答案:

Write the setTimeInterval this way. Otherwise the function gets executed in setTimeInterval. I know it is weird.

function InitiatePolling() {

        event.preventDefault();
        tId = setInterval(function(){Poll();}, 1000);
    }

Update: The problem is <%= PollResponse() %> this statement in you code. <%%> defines that code will be executed on server when the page HTML is generated. If you see the rendered HTML of you page on the browser you will see that it is not res = <%= PollResponse() %> but res = out put of PollResponse.

So this function is executed every time page is loaded.

If you want to make a call to server function from javascript you need to use ajax. something like this

function Poll() {
        event.preventDefault();
         $.ajax({
            url : "yourServerUrl/PollResponse",
            success : function(result){ res = result; }
         });        
    }

I am not aware of JS version to make asynchronous call. Please check that in the references.

网友答案:

You can try to use setTimeout instead of setInterval. Below is the code that works correctly:

        $(document).ready(function() {
            $('.btnSign').click(function() {
                Poll();
            });
        });

        function Poll() {
            debugger;            
            var res = '<%= PollResponse() %>';

            setTimeout(Poll, 2000);
        }
网友答案:

Try to use

$(document).ready(function() {
    $('#btnSign').click(function() {
        InitiatePolling();
    });
});

Anyway you should use browser developer tools and add a break point inside Poll function and check Call Stack tab to see from where it is being called.

分享给朋友:
您可能感兴趣的文章:
随机阅读: